Discussion Thread: DIP 1043--Shortened Method Syntax--Final Review

Paul Backus snarwin at gmail.com
Thu Jun 16 18:56:57 UTC 2022


On Thursday, 16 June 2022 at 18:15:52 UTC, Salih Dincer wrote:
> I don't need to write at length. The only thing I always have 
> difficulty with is writing a class constructor!
>
> ```d
> struct Color {}
> class Point {
>   Color rgba;
>   int x, y;
>   bool hidden;
>
>   this(Color r, int x, int y, bool h) {
>     this.rgba = r;
>     this.x = x;
>     this.y = y;
>     this.hidden = h;
>   }
> }
> ```
> Well if it was shorter we would compile it right away, just 
> like a struct. For example:
>
> ```d
>   // ...
>   this(@all);
> }
> ```
> SDB at 79

```d
enum defaultClassConstructor = q{
     this(typeof(this.tupleof) params)
     {
         static foreach (i; 0 .. this.tupleof.length)
         {
             this.tupleof[i] = params[i];
         }
     }
};

struct Color {}

class Point
{
     Color rgba;
     int x, y;
     bool hidden;

     mixin(defaultClassConstructor);
}

void main()
{
     Point p = new Point(Color(), 123, 456, false);
     assert(p.x == 123);
     assert(p.y == 456);
     assert(p.hidden == false);
}
```


More information about the Digitalmars-d mailing list