6 ms·
Speaking of which, coming from Rust (well, when I say it like that it sounds like Rust is the only language I ever used which is of course not true. I think Rus
by 65536 6y ago
Speaking of which, coming from Rust (well, when I say it like that it sounds like Rust is the only language I ever used which is of course not true. I think Rust as a first language would be highly unusual still), I enjoyed being able to say
Rust code:
let foo = if x <= 0
{
0
}
else if something_else > 50
{
9000
}
else
{
42
};
And I miss this in Swift.
The ternary operator is fine on its own but I’m not a huge fan of nested ternary operators.
With a bit of whitespace it's somewhat readable still but still not as comfortable and easy to read correctly.
Swift code:
let foo = x <= 0 ? 0 :
(somethingElse > 50 ? 9000 :
42 )
One way of writing something that is more similar to the way I'd write it in Rust would be to make a closure and then run it.
Swift code:
let foo =
{ () -> Int in
if x <= 0
{
return 0
}
else if somethingElse > 50
{
return 9000
}
else
{
return 42
}
}()
But it's a bit annoying still, both to read and to write. Also, even in the current version of Swift (Swift 5), the compiler cannot infer the return type of the closure on its own even though all of the branches return an Int, so I'd have to explicitly annotate that as I have done in the code above.
I guess for a lot of people they would just make foo mutable and write the code as
Swift code:
var foo = 42
if x <= 0
{
foo = 0
}
else if somethingElse > 50
{
foo = 9000
}
I concede that this is probably the most readable out of all of the three Swift code samples in my comment. But the point is that I didn't want foo to be mutable. I wanted to assign a value to it based on some simple logic and have it be immutable.
- w-ll 6y agoI'd guess i fall into the mutable camp, while sometimes doing a closure. Nested ternary if statements scare me from my PHP days, and noticed inlined closures can at times be harder to grok at a glance.
- bobbylarrybobby 6y agoI find ternary operators perfectly readable if you split the parts across lines — they map exactly to the conditions and statements in an if-else group. Cond1 // if this ? Result1 // return this : cond2 // else if this ? Result2 // return this : cond3 // else if this ? Result3 // return this : ResultElse // else return this
- pests 6y agoWhich is not true in PHP up until the most recent version (iirc) due to it being left associative instead of right associative you are assuming.
- amenod 6y agoTrue, or even: Cond1 // if this ? Result1 // return this : cond2 // else if this ? Result2 // return this : cond3 // else if this ? Result3 // return this : ResultElse // else return this
- giantDinosaur 6y agoGoodness me that has potential to become something truly hideous.
- Kkoala 6y agoIt's funny how people have so many different preferences for ternary formatting. I can't think of any other operator that would have tens of different format prefs.
- amenod 6y agoIt's actually not that bad in practice... But I guess the pragmatic formatting is "whatever `prettier` does by default" anyway. :)
- mekkkkkk 6y agoI've come to hesistently accept nested ternaries, but I try to have all lines lead with a colon. This kinda mimics a switch statement with returns. firstPossible ? thenThis : secondPossible ? thenThisOtherThing : thirdPossible ? thenThisThirdThing : defaultThing Always fun to see how people treat ternary nesting. They still feel a bit naughty to me. The terseness is sort of unbeatable though.
- Kkoala 6y ago
- hardwaregeek 6y agoAgreed. Plus if the else clause has a side effect, you can't assign it to the variable, which means you need to instantiate to null or a dummy value. Very messy. What's funny is that expression oriented languages translate really nicely to stack VMs. My hobby language compiles down to WASM and it was almost trivial to code gen if expressions.
- skocznymroczny 6y agoI dislike the ternary operator in languages like C++. And many languages copy-paste it because they are used to it. I much prefer the more literal and reversed form in Python. Instead of: int salary = isEmployed() ? 2000 : 0 I prefer: int salary = 2000 if isEmployed() else 0
- JimDabell 6y agoYes, it also makes the larger example somewhat readable: func calculate_tax(income): return 0 if income < 10 else 20 if income < 50 else 50
- jfkebwjsbx 6y agoI love that idea, but I still prefer it’d read the same as an if. Meaning the condition first and the false case last.
- nicoburns 6y agoRust implements this: let foo = if is_employed() { 2000 } else { 0 }
- jurip 6y agoAgreed about missing if and switch expressions in Swift. My preferred way of writing that would be: let foo: Int if x <= 0 { foo = 0 } else if somethingElse > 50 { foo = 9000 } else { foo = 42 } That way you get immutable foo and the compiler will shout at you if you forget to assign the value in one branch. It's not quite as nice as an expression, but it's less punctuation than the closure and the result is just as safe.
- GrayShade 6y agoIt's probably not the most idiomatic thing, but in Rust you can write: let foo = match () { _ if x <= 0 => 0, _ if something_else > 50 => 9000, _ => 42, };