digitalmars.D - if-expressions
- Cauterite (28/28) Aug 26 2016 Here's a little patch you guys might enjoy:
- Cauterite (3/4) Aug 26 2016 ehem...
- Bill Hicks (6/10) Aug 26 2016 Please stop trolling. D syntax is already sterile and verbose
- Basile B. (3/6) Aug 27 2016 May you directly show us examples ?
- Cauterite (3/3) Aug 27 2016 On Saturday, 27 August 2016 at 01:45:58 UTC, Bill Hicks wrote:
- vladdeSV (4/32) Aug 28 2016 Nice work!
- Cauterite (6/10) Aug 28 2016 Mm you're right, I had the same concern. The ':' or '?' could
- Tomer Filiba (3/8) Aug 28 2016 Python uses `x = 5 if cond else 6`, which is by far more readable
- Cauterite (14/17) Aug 28 2016 conseq1 if cond1 else conseq2 if cond2 else conseq3 if cond3 else
- pineapple (5/13) Aug 30 2016 Speaking of which, what's up with D's `do{ ... } while(...);`?
- Chris Wright (3/19) Aug 30 2016 Placing the condition at the end reflects the fact that the condition is...
- pineapple (3/5) Aug 30 2016 I'm aware of that, but the syntax is inconsistent and harder to
- w0rp (9/37) Aug 30 2016 I don't think this particular syntax is desirable. We already
- Cauterite (6/10) Aug 30 2016 The problem is when you're in the middle of a large expression
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
On Friday, 26 August 2016 at 18:25:00 UTC, Cauterite wrote:// any number of condition/predicate pairsehem... "any number of predicate:consequent pairs"
Aug 26 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.
Aug 26 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 ? :รพ
Aug 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.
Aug 27 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;`
Aug 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?
Aug 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
Aug 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 -tomerconseq1 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
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
On Tue, 30 Aug 2016 11:58:49 +0000, pineapple wrote:On Sunday, 28 August 2016 at 16:17:31 UTC, Cauterite wrote:Placing the condition at the end reflects the fact that the condition is executed at the end of the loop.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
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
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
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