if-expressions

vladdeSV via Digitalmars-d digitalmars-d at puremagic.com
Sun Aug 28 03:08:09 PDT 2016


On Friday, 26 August 2016 at 18:25:00 UTC, Cauterite wrote:
> Here's a little patch you guys might enjoy:
> https://github.com/dlang/dmd/compare/master...Cauterite:ifExpr0
>
> It enables this syntax:
> int foo = if(asdf: 5 else 6);
> equivalent to
> int foo = asdf ? 5 : 6;
>
> Here's some other examples which work:
>
> // any number of condition/predicate pairs
> foo = if(
>     asdf : 5,
>     doZxcv(bar) : 90
>     else 6
> );
>
> // redundant commas and colons permitted
> foo = if(
>     a : 5,
>     b : 90,
>     else : 6,
> );
>
> // roughly equivalent to
> // foo = asdf ? 5 : doZxcv(bar) ? 90 : assert(0);
> foo = if(
>     asdf : 5,
>     doZxcv(bar) : 90
> );
>
> Also it doesn't conflict with if-statement syntax, as far as 
> I'm aware.
>
>
> Just a little experiment to learn my way around the parser.


Nice work!

However, don't you think it's a bit odd that `if(asdf : <-- colon 
5 else 6)` equals `asdf ? <-- questionmark 5 : <-- colon 6;`



More information about the Digitalmars-d mailing list