Hi there!
Is there any good reason to not have an if only syntax available for ternary operator where the two following syntax would be equivalent: a ? b and a ? b : nothing ?
Hi there!
Is there any good reason to not have an if only syntax available for ternary operator where the two following syntax would be equivalent: a ? b and a ? b : nothing ?
Off the top of my head, one problem is then a ? 1:3 would be really weird (is 1:3 a range or a terinary?)
Space is enforced for ternary operator, so it would be a range ^^
EDIT: to the best of my limited knowledge, I don’t know of a case where we can use : with space beside for ternary operator, though my question
julia> 1 : 3
1:3
OK, my bad!
Well, thank you and sorry for the time!
I don’t see why nothing would be a useful thing to return.
If you are looking for something like a “one-line if”, there is
a = 4
a > 3 && println("Big")
The nothing matches what you get out of an if expression that doesn’t have an else branch.
I really do like the terseness of && and ||, but I always felt using them this way was a bit of a hack and I used to find it quite unintuitive to read or I would get the logic backwards, and I imagine I can’t be the only one.
As expressions they don’t behave quite like if expressions because the untaken branch returns a Bool instead of nothing. So I always felt there was scope for adding some additional syntax here. (But - more syntax makes for a more complex language, so I’m not sure where the balance lies on this one).
I think that at this point, they are pretty established and widely used in Julia packages.