digitalmars.D - Please support shorter constructor init
- Danilo Krahn (77/77) Jul 08 2023 Please add `this` and `super` to constructor parameters.
- FeepingCreature (16/43) Jul 08 2023 For simple code, you can use my boilerplate library
- Danilo Krahn (3/3) Jul 08 2023 The question was about the future of shortening the constructor
- FeepingCreature (14/17) Jul 08 2023 Well, I guess my answer would be "I don't think this is very
- Nick Treleaven (8/22) Jul 08 2023 Your mixin doesn't let the constructor also do something else.
- Andrej Mitrovic (16/18) Jul 10 2023 And for really simple cases you can just do:
- Bastiaan Veelo (27/28) Jul 08 2023 Note that for structs, there is no need to define a constructor
- Nick Treleaven (7/21) Jul 08 2023 That doesn't allow fields to be dynamically initialized
- IchorDev (3/12) Jul 09 2023 https://dlang.org/spec/struct.html#dynamic_struct_init
- Nick Treleaven (2/7) Jul 12 2023 It's dynamic, but it's not automatic.
- ryuukk_ (4/81) Jul 08 2023 Constructor on structs?
- Danilo Krahn (7/10) Jul 08 2023 This has nothing to do with "Vector" or "inheritance".
- ryuukk_ (15/28) Jul 08 2023 nobody should use constructor on structs
- Nick Treleaven (11/22) Jul 08 2023 It's not advice. It's explaining what the example code means with
- claptrap (16/17) Jul 08 2023 To get a new feature added to the language someone needs to write
- Danilo Krahn (2/17) Jul 08 2023 Thanks!
- a11e99z (3/4) Jul 09 2023 better to add records
Please add `this` and `super` to constructor parameters. Old style: ```d struct Rectangle { int x,y,width,height; Color color; this(int x, int y, int width, int height, Color color) { this.x = x; this.y = y; this.width = width; this.height = height; this.color = color; } } ``` New style. Shorter. Less to type: ```d struct Rectangle { int x,y,width,height; Color color; this(this.x, this.y, this.width, this.height, this.color) {} } ``` Constructor initialization is annoying and often repeating. Make it shorter. Nicer. `this` and `super` can be directly supported in constructor parameters. It would save lots of typing. The code: ```d module app; class Vector2D { float x, y; this(int x, int y) { this.x = x; this.y = y; } } class Vector3D : Vector2D { float z; this(int x, int y, int z) { super(x,y); this.z = z; } } void main() { auto v1 = new Vector2D(10,20); } ``` could be shortened to: ```d module app; class Vector2D { float x, y; this(this.x, this.y); } class Vector3D : Vector2D { float z; this(super.x, super.y, this.z); } void main() { auto v1 = new Vector2D(10,20); } ``` or ```d module app; class Vector2D { float x, y; this(this.x, this.y) {} } class Vector3D : Vector2D { float z; this(super.x, super.y, this.z) {} } void main() { auto v1 = new Vector2D(10,20); } ```
Jul 08 2023
On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:Please add `this` and `super` to constructor parameters. Old style: ```d struct Rectangle { int x,y,width,height; Color color; this(int x, int y, int width, int height, Color color) { this.x = x; this.y = y; this.width = width; this.height = height; this.color = color; } } ``` New style. Shorter. Less to type: ```d struct Rectangle { int x,y,width,height; Color color; this(this.x, this.y, this.width, this.height, this.color) {} } ``` Constructor initialization is annoying and often repeating. Make it shorter. Nicer. `this` and `super` can be directly supported in constructor parameters. It would save lots of typing.For simple code, you can use my boilerplate library https://code.dlang.org/packages/boilerplate ``` struct Rectangle { int x,y,width,height; Color color; mixin(GenerateThis); } Rectangle(2, 3, 10, 20, color) ``` If the superclass is also boilerplated, it'll automatically insert the super parameters too. Also for the love of all that is holy, use structs for vectors, not classes. The performance cost of the allocations will eat you alive.
Jul 08 2023
The question was about the future of shortening the constructor syntax inside the D programming language, not about mixins or struct/class differences.
Jul 08 2023
On Saturday, 8 July 2023 at 08:39:13 UTC, Danilo Krahn wrote:The question was about the future of shortening the constructor syntax inside the D programming language, not about mixins or struct/class differences.Well, I guess my answer would be "I don't think this is very important, since you can get similarly short constructors using mixins such as this library." Honestly, I'd like the language to just generate sensible constructors to begin with. We have default values on struct and class members, so why does the autogenerated constructor allow omitting fields that don't have a default value? The `this.x` syntax is very cute, I do like it (Neat uses it for its constructors) but it's sort of "neither here nor there". It's a syntax hack to make it easier to write a constructor that the language could just as easily generate on its own. Its only advantage is that it lets you reorder constructor parameters, but you can reorder class fields just as easily.
Jul 08 2023
On Saturday, 8 July 2023 at 08:52:29 UTC, FeepingCreature wrote:Well, I guess my answer would be "I don't think this is very important, since you can get similarly short constructors using mixins such as this library."Your mixin doesn't let the constructor also do something else. Also you have to import the module with the mixin which is an annoyance.Honestly, I'd like the language to just generate sensible constructors to begin with. We have default values on struct and class members, so why does the autogenerated constructor allow omitting fields that don't have a default value?Because you can also omit an initializer for a local variable. If you want to restrict field values, define a constructor.The `this.x` syntax is very cute, I do like it (Neat uses it for its constructors) but it's sort of "neither here nor there". It's a syntax hack to make it easier to write a constructor that the language could just as easily generate on its own.Not for `super`.Its only advantage is that it lets you reorder constructor parameters, but you can reorder class fields just as easily.You can't reorder struct fields without changing the layout.
Jul 08 2023
On Saturday, 8 July 2023 at 08:35:12 UTC, FeepingCreature wrote:For simple code, you can use my boilerplate library https://code.dlang.org/packages/boilerplateAnd for really simple cases you can just do: ```D class C { int x; string y; float z; this(typeof(C.tupleof) args) { this.tupleof = args; } } void main() { auto c = new C(1, "foo", 1.0); } ``` But I guess `boilerplate` supports more advanced features.
Jul 10 2023
On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:Please add `this` and `super` to constructor parameters.Note that for structs, there is no need to define a constructor if parameters map on fields one on one and the constructor does nothing else: ```d import std; enum Color {Blue, Red} struct Rectangle { int x,y,width,height; Color color; } void main() { auto r = Rectangle(4, 5, 6, 7, Color.Blue); writeln(r); // Rectangle(4, 5, 6, 7, Blue) } ``` And if they don't map one on one, the caller can use a static or dynamic initializer instead: ```d Rectangle r2 = {width:6, height:7, x:4, y:5}; writeln(r2); // Rectangle(4, 5, 6, 7, Blue) ``` For classes, though, this isn't available. Your proposal wouldn't work in a nested class, as `this` and `super` refer to the nesting class. -- Bastiaan.
Jul 08 2023
On Saturday, 8 July 2023 at 12:42:13 UTC, Bastiaan Veelo wrote:On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:...Please add `this` and `super` to constructor parameters.Note that for structs, there is no need to define a constructor if parameters map on fields one on one and the constructor does nothing else:And if they don't map one on one, the caller can use a static or dynamic initializer instead: ```d Rectangle r2 = {width:6, height:7, x:4, y:5}; writeln(r2); // Rectangle(4, 5, 6, 7, Blue) ```That doesn't allow fields to be dynamically initialized automatically, like constructors do.For classes, though, this isn't available. Your proposal wouldn't work in a nested class, as `this` and `super` refer to the nesting class.No, this and super refer to the nested class instance. You access the outer class instance using the `outer` property: https://dlang.org/spec/class.html#outer-property
Jul 08 2023
On Saturday, 8 July 2023 at 19:41:39 UTC, Nick Treleaven wrote:...https://dlang.org/spec/struct.html#dynamic_struct_init Is this not... "automatic"?And if they don't map one on one, the caller can use a static or dynamic initializer instead: ```d Rectangle r2 = {width:6, height:7, x:4, y:5}; writeln(r2); // Rectangle(4, 5, 6, 7, Blue) ```That doesn't allow fields to be dynamically initialized automatically, like constructors do.
Jul 09 2023
On Sunday, 9 July 2023 at 17:32:20 UTC, IchorDev wrote:It's dynamic, but it's not automatic.That doesn't allow fields to be dynamically initialized automatically, like constructors do.https://dlang.org/spec/struct.html#dynamic_struct_init Is this not... "automatic"?
Jul 12 2023
On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:Please add `this` and `super` to constructor parameters. Old style: ```d struct Rectangle { int x,y,width,height; Color color; this(int x, int y, int width, int height, Color color) { this.x = x; this.y = y; this.width = width; this.height = height; this.color = color; } } ``` New style. Shorter. Less to type: ```d struct Rectangle { int x,y,width,height; Color color; this(this.x, this.y, this.width, this.height, this.color) {} } ``` Constructor initialization is annoying and often repeating. Make it shorter. Nicer. `this` and `super` can be directly supported in constructor parameters. It would save lots of typing. The code: ```d module app; class Vector2D { float x, y; this(int x, int y) { this.x = x; this.y = y; } } class Vector3D : Vector2D { float z; this(int x, int y, int z) { super(x,y); this.z = z; } } void main() { auto v1 = new Vector2D(10,20); } ``` could be shortened to: ```d module app; class Vector2D { float x, y; this(this.x, this.y); } class Vector3D : Vector2D { float z; this(super.x, super.y, this.z); } void main() { auto v1 = new Vector2D(10,20); } ``` or ```d module app; class Vector2D { float x, y; this(this.x, this.y) {} } class Vector3D : Vector2D { float z; this(super.x, super.y, this.z) {} } void main() { auto v1 = new Vector2D(10,20); } ```Constructor on structs? Vectors as classes with inheritance? Sorry, but this is just bad advice that nobody should follow
Jul 08 2023
On Saturday, 8 July 2023 at 14:52:34 UTC, ryuukk_ wrote:Constructor on structs? Vectors as classes with inheritance? Sorry, but this is just bad advice that nobody should followThis has nothing to do with "Vector" or "inheritance". It's about repeating constructor initialization. Modern languages support direct init using `this` and `super` inside the parameters, so repeating `this.x = x` is not neccessary. https://dart.dev/language#classes
Jul 08 2023
On Saturday, 8 July 2023 at 15:33:34 UTC, Danilo Krahn wrote:On Saturday, 8 July 2023 at 14:52:34 UTC, ryuukk_ wrote:nobody should use constructor on structs and nobody should use classes for basic stuffs that should be simple pod ```d struct vec3 { float x,y,z; } auto v = vec3(1,2,3); ``` This already works ```d vec3 v = { x: 1, y: 2, z: 3 } ``` This also worksConstructor on structs? Vectors as classes with inheritance? Sorry, but this is just bad advice that nobody should followThis has nothing to do with "Vector" or "inheritance". It's about repeating constructor initialization. Modern languages support direct init using `this` and `super` inside the parameters, so repeating `this.x = x` is not neccessary. https://dart.dev/language#classes
Jul 08 2023
On Saturday, 8 July 2023 at 15:58:57 UTC, ryuukk_ wrote:On Saturday, 8 July 2023 at 15:33:34 UTC, Danilo Krahn wrote:It's not advice. It's explaining what the example code means with the new feature.On Saturday, 8 July 2023 at 14:52:34 UTC, ryuukk_ wrote:Constructor on structs? Vectors as classes with inheritance? Sorry, but this is just bad advice that nobody should follownobody should use constructor on structsThat's the first time I've ever heard that. https://dlang.org/phobos/std_typecons.html#.Nullable.this https://dlang.org/phobos/std_typecons.html#.Tuple.this https://dlang.org/phobos/std_typecons.html#.Unique.thisand nobody should use classes for basic stuffs that should be simple podWhat if you want a vector reference type on the heap, e.g. for immutable vectors to share across threads? `new Struct` would return a pointer, so `+=` won't work. Or if you want it to be `synchronized`?
Jul 08 2023
On Saturday, 8 July 2023 at 19:57:55 UTC, Nick Treleaven wrote:On Saturday, 8 July 2023 at 15:58:57 UTC, ryuukk_ wrote:All of these are poorly designed constructs Nullable? should be a tagged union or something built-in, tuple, should be built-in as wellOn Saturday, 8 July 2023 at 15:33:34 UTC, Danilo Krahn wrote:It's not advice. It's explaining what the example code means with the new feature.On Saturday, 8 July 2023 at 14:52:34 UTC, ryuukk_ wrote:Constructor on structs? Vectors as classes with inheritance? Sorry, but this is just bad advice that nobody should follownobody should use constructor on structsThat's the first time I've ever heard that. https://dlang.org/phobos/std_typecons.html#.Nullable.this https://dlang.org/phobos/std_typecons.html#.Tuple.this https://dlang.org/phobos/std_typecons.html#.Unique.thisThis is the result of poorly designed software, you always want to do more esoteric things; "the java way"and nobody should use classes for basic stuffs that should be simple podWhat if you want a vector reference type on the heap, e.g. for immutable vectors to share across threads? `new Struct` would return a pointer, so `+=` won't work. Or if you want it to be `synchronized`?
Jul 08 2023
On Saturday, 8 July 2023 at 20:54:14 UTC, ryuukk_ wrote:This is the result of poorly designed software, you always want to do more esoteric things; "the java way"To be fair, java is a really well designed language for its domain. It follows the idea of a simple language with a rich standard library, which is what I think OP is expecting of D.
Jul 09 2023
On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:Please add `this` and `super` to constructor parameters.To get a new feature added to the language someone needs to write a detailed "D improvement proposal" for it... https://github.com/dlang/DIPs It takes a lot of effort, so either you have to care enough about it to do it yourself or you need to find someone else who feels the same. All you get by asking for a feature in the forum is a bunch people yapping about it. Im only telling you this so you dont have unrealistic expectations regarding your request. And it is worth considering that it's probably something that's been suggested before, I dont remember for sure, but probably. And there's pretty much always bigger fish to fry. disclaimer : Im just a user, I dont speak in any official capacity.
Jul 08 2023
On Saturday, 8 July 2023 at 18:34:44 UTC, claptrap wrote:On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:Thanks!Please add `this` and `super` to constructor parameters.To get a new feature added to the language someone needs to write a detailed "D improvement proposal" for it... https://github.com/dlang/DIPs It takes a lot of effort, so either you have to care enough about it to do it yourself or you need to find someone else who feels the same. All you get by asking for a feature in the forum is a bunch people yapping about it. Im only telling you this so you dont have unrealistic expectations regarding your request. And it is worth considering that it's probably something that's been suggested before, I dont remember for sure, but probably. And there's pretty much always bigger fish to fry.
Jul 08 2023
On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:Please add `this` and `super` to constructor parameters.better to add records https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record
Jul 09 2023