Please support shorter constructor init
Bastiaan Veelo
Bastiaan at Veelo.net
Sat Jul 8 12:42:13 UTC 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.
More information about the Digitalmars-d
mailing list