www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Please support shorter constructor init

reply Danilo Krahn <anon dlang.de> writes:
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
next sibling parent reply FeepingCreature <feepingcreature gmail.com> writes:
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
next sibling parent reply Danilo Krahn <anon dlang.de> writes:
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
parent reply FeepingCreature <feepingcreature gmail.com> writes:
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
parent Nick Treleaven <nick geany.org> writes:
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
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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/boilerplate
And 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
prev sibling next sibling parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
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
parent reply Nick Treleaven <nick geany.org> writes:
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
parent reply IchorDev <zxinsworld gmail.com> writes:
On Saturday, 8 July 2023 at 19:41:39 UTC, Nick Treleaven wrote:
 ...
 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.
https://dlang.org/spec/struct.html#dynamic_struct_init Is this not... "automatic"?
Jul 09 2023
parent Nick Treleaven <nick geany.org> writes:
On Sunday, 9 July 2023 at 17:32:20 UTC, IchorDev wrote:
 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"?
It's dynamic, but it's not automatic.
Jul 12 2023
prev sibling next sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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
parent reply Danilo Krahn <anon dlang.de> writes:
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 follow
This 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
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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:
 Constructor on structs?

 Vectors as classes with inheritance?

 Sorry, but this is just bad advice that nobody should follow
This 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
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 works
Jul 08 2023
parent reply Nick Treleaven <nick geany.org> writes:
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:
 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 follow
It's not advice. It's explaining what the example code means with the new feature.
 nobody should use constructor on structs
That'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.this
 and nobody should use classes for basic stuffs that should be 
 simple pod
What 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
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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:
 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:
 Constructor on structs?

 Vectors as classes with inheritance?

 Sorry, but this is just bad advice that nobody should follow
It's not advice. It's explaining what the example code means with the new feature.
 nobody should use constructor on structs
That'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.this
All of these are poorly designed constructs Nullable? should be a tagged union or something built-in, tuple, should be built-in as well
 and nobody should use classes for basic stuffs that should be 
 simple pod
What 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`?
This is the result of poorly designed software, you always want to do more esoteric things; "the java way"
Jul 08 2023
parent Andrew <andrewlalisofficial gmail.com> writes:
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
prev sibling next sibling parent reply claptrap <clap trap.com> writes:
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
parent Danilo Krahn <anon dlang.de> writes:
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:
 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.
Thanks!
Jul 08 2023
prev sibling parent a11e99z <black80 bk.ru> writes:
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