Possible enhancement: Concise return statements

Timon Gehr timon.gehr at gmx.ch
Mon Aug 15 03:54:08 PDT 2011


Currently, when using delegates in D, there is 'return' all over the place.
Eg:
map!((a){return a*foo(a);})(arr);

Many delegates consist of only one return statement. Writing 'return' is 
rather inconvenient and adds to the general noise.

Now, I would like to propose to enhance the language in the following way:

'When the last ExpressionStatement in a function body is missing the 
';', it is implicitly returned.'

The example above would become:

map!((a){a*foo(a)})(arr);

Multi-statement delegates would also be supported:
{auto y=x;++x;y}();

Probably, it would be best to disallow to use both forms of return 
statement simultaneously:

{if(x) return y; z} // error

I feel that this would add much to the language for both code 
readability and pleasure of writing code, especially in a functional 
style. As it is a purely additive change (currently, such code is always 
a syntax error), no existing code would be broken.


(One indirect benefit would be that we could then require lazy arguments 
to be pure. Call by name would then be implemented by eg:

int x;
foo({x<100},{x++}); // it is evident at the call site what is going on)



Any thoughts on this?



More information about the Digitalmars-d mailing list