proposal: short => rewrite for function declarations

Adam D. Ruppe destructionator at gmail.com
Fri Oct 9 17:50:54 UTC 2020


On Friday, 9 October 2020 at 17:40:56 UTC, Andre Pany wrote:
> If I remember correctly, the property setter in your example 
> works, because the assignment _x = v returns an int.
> In case you do not assign the value but call a method which has 
> return type void, it won't work? (As far as I remember).

Well, it will actually sometimes work.

Remember it just rewrites => x; into { return x; } And dmd 
*sometimes* lets you:

void foo() {}

void test() {
     return foo();
}


It sees you are just returning void from a void function and 
allows it.

If it is allowed there,

void test() => foo();

is also allowed.

But `void b() => _x = 5;` triggers "Error: cannot return non-void 
from void function", the same as if you wrote out `void b() { 
return _x = 5; }`


If you don't want to return anything of course you can just write 
the bracket syntax:

// this is allowed, but why would you when you can just write...
void foo() => cast(void)(x+=5);
// this instead?
void foo() { x+= 5; }


You only really save syntax if you are returning something anyway.


More information about the Digitalmars-d mailing list