digitalmars.D.learn - Why isn't field-wise constructor automatic for structs and not classes?
- Shriramana Sharma (11/11) Jan 01 2016 If I have:
- John Colvin (4/13) Jan 02 2016 Strictly speaking you aren't calling a constructor there, you're
- Shriramana Sharma (6/8) Jan 02 2016 Why do you say I'm not calling a constructor?
- rumbu (12/18) Jan 02 2016 A class constructor is written as:
- John Colvin (7/13) Jan 02 2016 https://dlang.org/spec/struct.html#struct-literal
If I have: struct TimeSpan { double start, end; } Then both the following automatically work: auto s = TimeSpan(); auto t = TimeSpan(1, 2); But if I make it a class (I need to) then I have to explicitly define a field-wise constructor else only a constructor with no args is automatically defined. Why can't the field-wise functionality be automatic for classes too? --
Jan 01 2016
On Saturday, 2 January 2016 at 02:12:19 UTC, Shriramana Sharma wrote:If I have: struct TimeSpan { double start, end; } Then both the following automatically work: auto s = TimeSpan(); auto t = TimeSpan(1, 2); But if I make it a class (I need to) then I have to explicitly define a field-wise constructor else only a constructor with no args is automatically defined. Why can't the field-wise functionality be automatic for classes too?Strictly speaking you aren't calling a constructor there, you're writing a struct literal.
Jan 02 2016
John Colvin wrote:Strictly speaking you aren't calling a constructor there, you're writing a struct literal.Why do you say I'm not calling a constructor? And that still doesn't answer the question of why can't we have an automatic field-wise constructor for classes... --
Jan 02 2016
On Saturday, 2 January 2016 at 14:57:58 UTC, Shriramana Sharma wrote:John Colvin wrote:A class constructor is written as: auto s = *new* Timespan(1, 2);Strictly speaking you aren't calling a constructor there, you're writing a struct literal.Why do you say I'm not calling a constructor?And that still doesn't answer the question of why can't we have an automatic field-wise constructor for classes...Probably because the inheritance: class C1 { int x, y; } class C2 : C1 { int z; } How the C2 default memberwise constructor would look like? new C2(x, y)? or new C2(x, y, z)? What if x and y are private or reintroduced as public members in C2? I think a default memberwise constructor for classes will break the encapsulation paradigm of OOP programming.
Jan 02 2016
On Saturday, 2 January 2016 at 14:57:58 UTC, Shriramana Sharma wrote:John Colvin wrote:https://dlang.org/spec/struct.html#struct-literalStrictly speaking you aren't calling a constructor there, you're writing a struct literal.Why do you say I'm not calling a constructor?And that still doesn't answer the question of why can't we have an automatic field-wise constructor for classes...Classes aren't as simple as structs, they have hidden members, inherited members... Technically speaking the compiler is even allowed to change the ordering. They may be other reasons I'm not aware of / aren't thinking of right now.
Jan 02 2016