Optional braces

Timon Gehr timon.gehr at gmx.ch
Fri Aug 19 05:16:08 PDT 2011


On 08/19/2011 08:50 AM, Jacob Carlborg wrote:
> This might sound like a crazy idea. I don't expect everyone to drop what
> they're doing and start to implement this, it's just an idea.
>
> In Java (C, C++ and others) braces are optional for statements like if,
> while, for and so on, containing only one expression. In D this is true
> as well, but compared with Java, D also allow try, catch and finally
> statements to have optional braces.
>
> What about extending this to allow optional braces everywhere, where the
> braces contain a single expression? I'm thinking this will be most
> useful for functions/methods, templates and delegates. Specially for
> methods acting as properties that just forwards a value.
>
> Examples:
>
> class Foo
> {
> private int a_;
> int a () return a_;
> int a (int a) return a_ = a;
> }


I think this makes code harder to read for no obvious benefit.
I don't think this is any better than

class Foo{
     private int a_;
     int a(){return a_;}
     int a(int a){return a_ = a;}
}

>
> template Bar (T) alias T FooBar;

This one I'd probably use.

>
> auto foo = delegate () return 3;
> auto bar = return 3; // don't if this would be possible
{return 3;} is better than both of them imo.

>



More information about the Digitalmars-d mailing list