Domanda

What is the difference between declaring a constructor

this(...) { ... }

and

init(...) { ... }

for D struct?

I wonder because BitArray in std.bitmanip only uses init().

È stato utile?

Soluzione

It is because D1 didn't have struct constructors. std.bitmanip's BitArray is a port of D1's std.bitarray module http://www.digitalmars.com/d/1.0/phobos/std_bitarray.html, so it is written in a D1 style and never really updated to use new language features.

Contrast D1's struct feature table: http://www.digitalmars.com/d/1.0/struct.html

with D2's http://dlang.org/struct.html

and you'll see quite a few differences - the D1 struct was essentially a C struct with a little bit of syntax sugar for methods. Since constructors were impossible, you used init methods instead. (Similarly, D1 didn't have struct destructors either, making C++ style idioms like RAII pretty much unworkable - you would have to use scope(exit) mystruct.destroy(); style code instead.)

So it is just a historical thing. Constructors, under the hood, work the same way as these init methods.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top