There is an operator ? :
in Java which can be used to select a value according to the boolean expression. For example, the expression 3 > 2 ? "true" : false
will return a string "true"
. I know we can use if
expression to do this, but I will prefer this style because it is concise and elegant.

- 4,298
- 9
- 47
- 66
-
2Why don't you ask the Scala developers? They are the only people who really know ... and the people you would need to get to change their minds. – Stephen C Nov 23 '11 at 06:40
-
2There's an older question about a ternary operator in scala here: http://stackoverflow.com/questions/4947535/ternary-operator-similar-to – flesk Nov 23 '11 at 06:44
-
Not going to close, simply because others might be able to make the `?:` -> `if/else` connection... but the question, as it's worded, isn't exactly "within the FAQ guidelines". – Nov 23 '11 at 07:29
-
IMO not having a ternary operator is a poor decision by the scala developers if they're hoping for people from java and other languages to convert over easily to using it. When I go to write some conditional interpolation in a string I want to use ternary's for conciseness, the `if/else` really doesn't fit into that. Just my opinion, but removing something standard like the ternary operator that's present in java, php, c, c++, etc is a frustrating gotcha I imagine a lot of people run into. – EdgeCaseBerg Aug 07 '15 at 16:22
3 Answers
In Java, there is a difference between if
and ? :
and that is that if
is a statement while ? :
is an expression. In Scala, if
is also an expression: it returns a value that you can for example assign to a variable.
The if
in Scala is much more like ? :
in Java than the if
in Java:
// In Scala 'if' returns a value that can be assigned to a variable
val result = if (3 > 2) "yes" else "no"
You cannot do this in Java:
// Illegal in Java, because 'if' is a statement, not an expression
String result = if (3 > 2) "yes" else "no"
So, it is really not necessary to have ? :
in Scala because it would be exactly the same as if
, but with alternative (more obscure) syntax.

- 202,709
- 46
- 318
- 350
-
10
-
1
-
Then why design the language to make `?` and `:` conflicting in the first place? How is `? :` for ternary conditional operator less readable than `?` for ask (as readability if often cited as the reason for such design)? – Michal M Nov 02 '15 at 15:50
-
the ternary operator is not obscure. millions of C, C++, and Java devs understand the syntax. stating that it is obscure is an opinion, contrary to evidence. – ChuckCottrill Sep 29 '16 at 14:55
-
@ChuckCottrill There are multiple definitions of the word "obscure." Among these is "difficult to understand" ([source](https://en.wiktionary.org/wiki/obscure)). It is debatably harder to read code written with the C-style ternary operator. – Brian McCutchon Sep 14 '17 at 06:01
To add to what @Jesper said, if you find if
-else
too verbose, Scalaz provides two more terser alternatives: A ternary boolean operator (?
, |
) a la C-like languages, and fold
function.
scala> import scalaz._
import scalaz._
scala> import Scalaz._
import Scalaz._
scala> (3 > 2) ? "True" | "False"
res59: java.lang.String = True
scala> (3 > 2).fold("True", "False")
res60: java.lang.String = True

- 90,905
- 62
- 285
- 365
-
What's the rationale of naming it `fold`? It doesn't seem anything like a FP fold. – Dan Burton Nov 23 '11 at 23:26
-
1@DanBurton: You may find the answer [here](http://blog.tmorris.net/debut-with-a-catamorphism/). – missingfaktor Nov 24 '11 at 05:17
-
-
@tutuca, here's the new link - http://tmorris.net/posts/debut-with-a-catamorphism/index.html. – missingfaktor May 03 '14 at 19:40
In approximate order of importance:
1) The :
symbol is reserved for type annotation
2) In Scala, symbols can be used as identifiers for method and value names. There are only a limited number of these available on a standard keyboard, so if you decide to make two of these into keywords, you're reducing the pool of operators that can be used (see footnote for why they'd have to be keywords)
3) As Jesper says, the more powerful if
/ else
already fulfil this role, so the gain is marginal
4) Scala also has pattern matching which fulfils a similar role with the match
keyword, in a much more general way. E.g. what if your expression evaluates to a something that is not a boolean?
5) It would add additional compiler complexity. Scala prefers simplicity and uniformity to special cases
6) It's a hangover from C, and is really quite an odd syntax. Scala has enough odd-looking syntax
Footnote:
While it's possible to come up with a reasonable scheme to emulate ? :
as methods (see Ternary operator typing), it's not as viable as keywords. It is complex to handle typing properly when the "true" and "false" alternatives are of different numberic types, operator precedence is a problem since you need operators that are lower priority than anything else (or else you have to use parentheses), and performance will likely suffer due to the runtime nature of the implementation.

- 1
- 1

- 50,650
- 20
- 113
- 180