On 07/25/2015 02:19 PM, Andrei Alexandrescu wrote:
On 7/23/15 5:26 PM, Ziad Hatahet via Digitalmars-d wrote:
On Thu, Jul 23, 2015 at 2:00 PM, Adam D. Ruppe via Digitalmars-d
<digitalmars-d@puremagic.com <mailto:digitalmars-d@puremagic.com>> wrote:

    I think it is actually kinda pretty:


What about:

int median(int a, int b, int c) {
     return (a<b) ? (b<c) ? b : (a<c) ? c : a : (a<c) ? a : (b<c) ? c
: b;
}

vs.

def median(a: Int, b: Int, c: Int) =
   if (a < b) {
     if (b < c) b
     else if (a < c) c
     else a
   }
   else if (a < c) a
   else if (b < c) c
   else b

This is a wash. If we want to discuss good things in Rust

(The quoted bit is Scala code.)

we could get inspiration from, we need relevant examples. -- Andrei


What do you mean?

I think it is pretty obvious that 'if'/'else' is "better" syntax than '?:'. It e.g. does not leave the separation of context and condition up to operator precedence rules and is hence easier to parse by a human. Not that I'd care much, but it is inconvenient to be asked not to use the ternary operator in team projects just because it has a badly engineered syntax.

Also, we have (int x){ return r; }, auto foo(int x){ return r; }, (int x)=>r, but not auto foo(int x)=>r. It's an arbitrary restriction.


(BTW: To all the people who like to put the ternary operator condition into parens in order to imitate if: A convention that makes more sense here is to put the entire (chained) ternary expression in parentheses.)

Reply via email to