On 15.08.2011 12:54, Timon Gehr wrote:
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.'

Seems a bit error prone. I'd rather have a rule that, "If the delegate body only consists of a single expression, it's implicitly returned".


The example above would become:

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

"a*foo(a);"

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

Error, need return y;

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

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

Error, use "x ? y : z;"

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?


Reply via email to