June 16, 2022

On Wednesday, 15 June 2022 at 09:21:58 UTC, Mike Parker wrote:

>

[..] in the Reviewer Guidelines (and listed at the bottom of this post).

https://github.com/dlang/DIPs/blob/master/docs/guidelines-reviewers.md

I don't need to write at length. The only thing I always have difficulty with is writing a class constructor!

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:

  // ...
  this(@all);
}

SDB@79

June 16, 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!

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:

  // ...
  this(@all);
}

SDB@79

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);
}
June 17, 2022
On 16.06.22 20:56, Paul Backus wrote:
>      this(typeof(this.tupleof) params)
>      {
>          static foreach (i; 0 .. this.tupleof.length)
>          {
>              this.tupleof[i] = params[i];
>          }
>      }

You don't need the loop:

    this(typeof(this.tupleof) params)
    {
        this.tupleof = params; /* works */
    }
June 17, 2022

On Thursday, 16 June 2022 at 18:56:57 UTC, Paul Backus wrote:

>

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

mixin(defaultClassConstructor);

}

It could also allow skipping some fields and support custom initialization of those fields using a function literal:

// declare `this(rgba, x, y)`, no `hidden` parameter
mixin defaultClassCtor!("!hidden", { hidden = ...; });

The function literal would run after the default assignments. Alternatively the parameters to use could be listed:

// declare `this(rgba, x, y)`, no `hidden` parameter
mixin defaultClassCtor!(q{rgba, x, y}, { hidden = ...; });

It could skip private fields by default, or include them with a flag:

// include private fields
mixin defaultClassCtor!(Yes.privateFields);

It could support default arguments:

mixin defaultClassCtor!(q{x = 0, y = 10});

And ddoc works for a documented template mixin declaration containing a documented constructor.

June 17, 2022

On Friday, 17 June 2022 at 13:17:37 UTC, Nick Treleaven wrote:

>

It could also allow skipping some fields and support custom initialization of those fields using a function literal:

[...]

>

And ddoc works for a documented template mixin declaration containing a documented constructor.

Unfortunately you pretty much have to use a string mixin, to avoid issue 3332.

For a more sophisticated implementation, GenerateThis from the boilerplate package is probably the way to go.

June 17, 2022

On Thursday, 16 June 2022 at 18:56:57 UTC, Paul Backus wrote:

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

Thanks, it looks very short...

SDB@79

June 17, 2022
On 6/15/22 11:21, Mike Parker wrote:
> Express your support

I support this DIP. Named functions should be at as easy to write as function literals. Apparently I have brought this up back in 2011 (IIRC this was shortly after the feature was added for function literals): https://issues.dlang.org/show_bug.cgi?id=7176
June 17, 2022
On 6/17/22 19:08, Salih Dincer wrote:
> On Thursday, 16 June 2022 at 18:56:57 UTC, Paul Backus wrote:
>> ```d
>> enum defaultClassConstructor = q{
>>  this(typeof(this.tupleof) params) {
>>    static foreach (i; 0..this.tupleof.length)
>>      this.tupleof[i] = params[i];
>> };
>> ```
> 
> Thanks, it looks very short...
> 
> SDB@79

```d
enum defaultClassConstructor = q{
    this(typeof(this.tupleof) params){
        this.tupleof = params;
    }
};
```
June 17, 2022

On Wednesday, 15 June 2022 at 09:21:12 UTC, Mike Parker wrote:

>

Here in the discussion thread, you are free to discuss anything and everything related to the DIP. Express your support or

I strongly support this DIP as it makes function definition syntax more consistent with lambdas. It also avoids brace nesting which is visually noisy. The lowering is easy to understand with no special cases. The updated DIP is well written and answered the points raised from the initial review.

June 17, 2022

On Wednesday, 15 June 2022 at 13:40:20 UTC, Paul Backus wrote:

>

On Wednesday, 15 June 2022 at 09:35:37 UTC, bauss wrote:

>

I'm still under the belief that it shouldn't just be AssignExpression, because you should be able to use it to call another function that has no return type.

Ex.

void a(int x, int y) { ... }

void b(int x) => a(x, 100);

This already works--both for the new syntax, and for the existing arrow-lambda syntax, and even for functions with an explicit return statement:

void a(int x, int y) { /* ... */ }
void b(int x) => a(x, 100);
void function(int) c = x => a(x, 100);
void d(int x) { return a(x, 100); }

There is no need to specify this in the DIP because it is already documented in the language spec, under the description of return statements:

>

An Expression of type void is allowed if the function specifies a void return type. The Expression will be evaluated, but nothing will be returned.

https://dlang.org/spec/statement.html#return-statement

Oh wow! Doing if () { writeln(); return; } has been a pet peeve of mine almost since I started programming. I never knew you could do if() return writeln() in D. That made my day. Thanks!