digitalmars.D.learn - Struct Constructors
- Mark Blume (9/9) Jun 16 2014 Why exactly isn't a constructor without any parameters is not
- Mark Blume (3/12) Jun 16 2014 I should also say that I checked out the documentation already,
- Dicebot (8/17) Jun 16 2014 http://stackoverflow.com/questions/16648273/why-can-i-not-implement-defa...
- Adam D. Ruppe (25/28) Jun 16 2014 The idea is that declaring a plain struct always has almost zero
- Mark Blume (3/3) Jun 16 2014 Adam, Dicebot, Thank you very much for the fast reply. I think
Why exactly isn't a constructor without any parameters is not allowed? Why does "Struct()" calls "Struct.opCall()," which means "Struct.init" initially, while "Struct(params)" calls "Struct.this(params)?" Does "Struct(params)" also call "Struct.opCall(params)?" I am new to D and I can't seem to understand this.
Jun 16 2014
On Monday, 16 June 2014 at 22:03:28 UTC, Mark Blume wrote:Why exactly isn't a constructor without any parameters is not allowed? Why does "Struct()" calls "Struct.opCall()," which means "Struct.init" initially, while "Struct(params)" calls "Struct.this(params)?" Does "Struct(params)" also call "Struct.opCall(params)?" I am new to D and I can't seem to understand this.I should also say that I checked out the documentation already, but there is not an actual explanation there.
Jun 16 2014
On Monday, 16 June 2014 at 22:03:28 UTC, Mark Blume wrote:Why exactly isn't a constructor without any parameters is not allowed? Why does "Struct()" calls "Struct.opCall()," which means "Struct.init" initially, while "Struct(params)" calls "Struct.this(params)?" Does "Struct(params)" also call "Struct.opCall(params)?" I am new to D and I can't seem to understand this.Right now I think this decision was not worth it and at least CTFE-only constructors should be allowed. But this is not a simple thing to change. Fun fact : originally in D1 days (long time ago in a galaxy far away) constructors were not allowed for structs at all. Good it has been changed :)
Jun 16 2014
On Monday, 16 June 2014 at 22:03:28 UTC, Mark Blume wrote:Why exactly isn't a constructor without any parameters is not allowed?The idea is that declaring a plain struct always has almost zero cost - it is just static data with no extra code run. A zero-arg constructor (aka a default constructor in C++) that is run automatically on the declaration breaks that idea. Since sometimes people want it anyway the compromise is to make a static opCall and explictly use it with parenthesis. This was introduced before D had any struct constructors at all. Then struct constructors were added to D later and they override the old static opCall method... but they must have at least one parameter. So short answer is the confusion is caused by two historical features that basically did the same thing. Nowadays, static opCall should mostly be avoided. Use constructors instead. Only if you need a zero arg constructor should you consider static opCall.Does "Struct(params)" also call "Struct.opCall(params)?"If there's no constructor, i think it can. But like i said above, you should avoid this because it is mostly just historical baggage. Use constructors. BTW non-static opCall is a different story, that can be useful for defining functor objects. That only works on an instance object though: struct Foo { void opCall() { } } Foo foo; // doesn't call any code, just plain variable declaration foo(); // calls foo.opCall();
Jun 16 2014
Adam, Dicebot, Thank you very much for the fast reply. I think this should be addressed at the documentation since it has ability to cause confusion for newbies like me.
Jun 16 2014