if-expressions

w0rp via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 30 00:41:35 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.

I don't think this particular syntax is desirable. We already 
have ternary expressions, and anything more complicated than a 
regular ternary should probably be written with a regular series 
of if statements.

Having said that, it is good that you're trying to figure out how 
the compiler works. Keep doing that! You might be able to open a 
pull request for something else which is really valuable if you 
keep tinkering.


More information about the Digitalmars-d mailing list