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

WebFreak001 d.forum at webfreak.org
Tue Jun 28 11:25:53 UTC 2022


On Wednesday, 15 June 2022 at 09:21:12 UTC, Mike Parker wrote:
> This is the discussion thread for the Final Review of DIP 1043, 
> "Shortened Method Syntax":
>
> https://github.com/dlang/DIPs/blob/2c2f6c33f5761236266a96bd268c62a06323a5e8/DIPs/DIP1043.md
>
> [...]

I am a strong supporter of this change, I think this will help 
adoption a lot with several language and library constructs, 
especially with things like pattern matching with `std.sumtype`.

I have already used the preview a bit in some of my code and I 
think it's a very good addition to do things like

```d
int width() => shape.match!(
     (Box b) => b.width,
     (Circle c) => c.radius * 2
);

string name()          => shape.match!(s => s.name);
double calculateArea() => shape.match!(s => s.calculateArea);
```

rather than

```d
int width()
{
     return shape.match!(
         (Box b) => b.width,
         (Circle c) => c.radius * 2
     );
}

string name()
{
     return shape.match!(s => s.name);
}

double calculateArea()
{
     return shape.match!(s => s.calculateArea);
}
```

I think the readability (understanding) is just as good as with 
the extended code, but it's much cleaner to look at, especially 
when there are many of these methods in sequence.

I think this DIP would also increase property usage through 
methods.

For tooling I don't see any problem, it's already implemented in 
libdparse and D-Scanner makes use of it already too. The syntax 
is consistent with lambdas and actually feels like it could have 
been in there since the start.


More information about the Digitalmars-d mailing list