Please support shorter constructor init

Nick Treleaven nick at geany.org
Sat Jul 8 19:41:39 UTC 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


More information about the Digitalmars-d mailing list