Please support shorter constructor init

FeepingCreature feepingcreature at gmail.com
Sat Jul 8 08:35:12 UTC 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.


More information about the Digitalmars-d mailing list