www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - if-expressions

reply Cauterite <cauterite gmail.com> writes:
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.
Aug 26 2016
next sibling parent Cauterite <cauterite gmail.com> writes:
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"
Aug 26 2016
prev sibling next sibling parent reply Bill Hicks <billhicks gmail.com> writes:
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.
Aug 26 2016
next sibling parent Basile B. <b2.temp gmx.com> writes:
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 ? :รพ
Aug 27 2016
prev sibling parent Cauterite <cauterite gmail.com> writes:
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.
Aug 27 2016
prev sibling next sibling parent reply vladdeSV <v vladde.net> writes:
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;`
Aug 28 2016
parent reply Cauterite <cauterite gmail.com> writes:
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?
Aug 28 2016
parent reply Tomer Filiba <tomerfiliba gmail.com> writes:
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
Aug 28 2016
parent reply Cauterite <cauterite gmail.com> writes:
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.
Aug 28 2016
parent reply pineapple <meapineapple gmail.com> writes:
On Sunday, 28 August 2016 at 16:17:31 UTC, Cauterite wrote:
 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?
Speaking of which, what's up with D's `do{ ... } while(...);`? Why not improve upon the convention and write `dowhile(...){}` or just `do(...){}`, or really anything that doesn't put the condition at the end?
Aug 30 2016
parent reply Chris Wright <dhasenan gmail.com> writes:
On Tue, 30 Aug 2016 11:58:49 +0000, pineapple wrote:

 On Sunday, 28 August 2016 at 16:17:31 UTC, Cauterite wrote:
 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?
Speaking of which, what's up with D's `do{ ... } while(...);`? Why not improve upon the convention and write `dowhile(...){}` or just `do(...){}`, or really anything that doesn't put the condition at the end?
Placing the condition at the end reflects the fact that the condition is executed at the end of the loop.
Aug 30 2016
parent pineapple <meapineapple gmail.com> writes:
On Tuesday, 30 August 2016 at 13:38:51 UTC, Chris Wright wrote:
 Placing the condition at the end reflects the fact that the 
 condition is executed at the end of the loop.
I'm aware of that, but the syntax is inconsistent and harder to read.
Aug 30 2016
prev sibling parent reply w0rp <devw0rp gmail.com> writes:
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.
Aug 30 2016
parent Cauterite <cauterite gmail.com> writes:
On Tuesday, 30 August 2016 at 07:41:35 UTC, w0rp wrote:
 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.
The problem is when you're in the middle of a large expression which you don't want to break apart into statements (it might be the body of a '=>' lambda), your options are ternary conditionals (horrid syntax), or something like `{if (x) {return y;} else {return z;};}()` which is just as bad.
Aug 30 2016