Lambda syntax for methods and functions

bearophile bearophileHUGS at lycos.com
Sat Dec 7 10:13:04 PST 2013


MattCoder:

> Maybe It's just me, but on the example above I don't see too 
> much improvement:
>
> 1 word off ("return"), and "=>" instead of brackets.

In D:

Point move(in int dx, in int dy) pure nothrow {
     return Point(X + dx, Y + dy);
}

Point move(in int dx, in int dy) pure nothrow => Point(X + dx, Y 
+ dy);

It saves you almost 10% of typing, and makes more visible that 
move is an expression. If you use UFCS chains a lot, many 
functions become small and they sometimes contain just an 
expression.

Currently in D you can write:

enum move = (in int dx, in int dy) pure nothrow => Point(X + dx, 
Y + dy);

But here you can't specify the return value, this sometimes is a 
problem, because you miss the implicit cast at the return point 
(char[] -> string in pure functions, char -> dchar, Point -> 
const(Point), etc).

Bye,
bearophile


More information about the Digitalmars-d mailing list