Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ternary (?:) operator vs just using if-else-end #22692

Closed
saolof opened this issue Jul 6, 2017 · 1 comment
Closed

Ternary (?:) operator vs just using if-else-end #22692

saolof opened this issue Jul 6, 2017 · 1 comment

Comments

@saolof
Copy link

saolof commented Jul 6, 2017

Since If blocks are expressions, Julia's ternary operator is redundant since it is just a less readable alias for an if block. Furthermore, since Julia uses both : as a separate operator it leads to ambiguous parses in cases like whichrange()?1:2:3:4 (which returns 1 or 2:3:2 in Julia 0.6).

Deprecating the ?: syntax in favor of just pointing people to if blocks would make the language more consistent, it would eliminate the possibility of an ambiguous parse, and it would mean that people coming from Python or Matlab would have one less new bit of syntax to learn. Using if as a ternary expression is natural enough that I don't think that having the same syntax as C is of any benefit tbh.

Regarding the same tradeoff in other languages, Rust had both if expressions and ?: in beta, but deprecated ?: before 1.0. When someone put up a post-1.0 pull request in favor of adding (?:) it was overwhelmingly voted against
( rust-lang/rust#1698
rust-lang/rust#1705
rust-lang/rfcs#1362 ).

@nalimilan
Copy link
Member

See discussion at #16389. Closing as a duplicate since the two questions cannot be addressed separately (and honestly, I think that ship has sailed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants