Jump to page: 1 2
Thread overview
if-expressions
Aug 26, 2016
Cauterite
Aug 26, 2016
Cauterite
Aug 27, 2016
Bill Hicks
Aug 27, 2016
Basile B.
Aug 27, 2016
Cauterite
Aug 28, 2016
vladdeSV
Aug 28, 2016
Cauterite
Aug 28, 2016
Tomer Filiba
Aug 28, 2016
Cauterite
Aug 30, 2016
pineapple
Aug 30, 2016
Chris Wright
Aug 30, 2016
pineapple
Aug 30, 2016
w0rp
Aug 30, 2016
Cauterite
August 26, 2016
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.
August 26, 2016
On Friday, 26 August 2016 at 18:25:00 UTC, Cauterite wrote:
> // any number of condition/predicate pairs

ehem...
"any number of predicate:consequent pairs"
August 27, 2016
On Friday, 26 August 2016 at 18:25:00 UTC, Cauterite wrote:
> 
> 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.

Please stop trolling.  D syntax is already sterile and verbose enough, we don't need to make it uglier.  It's actually much easier to write ugly looking code in D than most other programming languages.  Just do a search on Github and see for yourself.
August 27, 2016
On Saturday, 27 August 2016 at 01:45:58 UTC, Bill Hicks wrote:
> It's actually much easier to write ugly looking code in D than most other programming languages.  Just do a search on Github and see for yourself.

May you directly show us examples ?

:รพ
August 27, 2016
On Saturday, 27 August 2016 at 01:45:58 UTC, Bill Hicks wrote:
>

Yes, fully implementing new language syntax is how I troll on the internet.


August 28, 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;`

August 28, 2016
On Sunday, 28 August 2016 at 10:08:09 UTC, vladdeSV wrote:
> 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;`

Mm you're right, I had the same concern. The ':' or '?' could both be just as easily used here, but I figured since IfExpressions kind of make ternary conditionals obsolete, it'd be better to 'free-up' the '?' token for other purposes. Maybe you have some other ideas?
August 28, 2016
On Sunday, 28 August 2016 at 11:14:00 UTC, Cauterite wrote:
> Mm you're right, I had the same concern. The ':' or '?' could both be just as easily used here, but I figured since IfExpressions kind of make ternary conditionals obsolete, it'd be better to 'free-up' the '?' token for other purposes. Maybe you have some other ideas?

Python uses `x = 5 if cond else 6`, which is by far more readable

-tomer
August 28, 2016
On Sunday, 28 August 2016 at 13:48:43 UTC, Tomer Filiba wrote:
> Python uses `x = 5 if cond else 6`, which is by far more readable
>
> -tomer

conseq1 if cond1 else conseq2 if cond2 else conseq3 if cond3 else conseq4

I dunno man, it seems all backwards to me. If you're gonna do it this way, then you'd also want your if-statements like this:

{
    foo();
    bar();
} if (cond);

Could you imagine trying to read a function you didn't write yourself if branches were written like that?

But more importantly IMO the order of execution should be reflected in the syntax. Even if you put the condition in the middle, it still gets executed first.
August 30, 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.
« First   ‹ Prev
1 2