Possible enhancement: Concise return statements

bearophile bearophileHUGS at lycos.com
Mon Aug 15 10:53:23 PDT 2011


Timon Gehr:

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

I remember others in past suggest the same idea. This is a good sign because it means it's a natural enough syntax for D programmers.


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

You have picked a suboptimal example, because D/Phobos already allows you write this (because the delegate uses only global names beside its arguments):

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


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

Optional return keyword for multi-statement code is not a good idea in D, despite similar blocks give in Ruby. Decreasing tidiness for a small syntactical convenience is often a bad idea.


> Any thoughts on this?

Time ago someone (me too) has suggested a syntax like this:
map!({ a => a * foo(a) })(arr);

To replace this syntax:
map!((a){ return a * foo(a); })(arr);

But overall I don't see this as a large improvement. It's just nice. Maybe for D3.

In my opinion a good "yield" (and generators) is syntax sugar that's rather more important than optionally omitting return. It changes the way you write code. Omitting a return doesn't change my way of using delegates, it just makes it a bit shorter.

Bye,
bearophile


More information about the Digitalmars-d mailing list