docs.scala-lang
docs.scala-lang copied to clipboard
"If as expression" needs some clarification
I suggest dropping this near "if as an expression" in Control structures
- if construct becomes (will be inferred) as expression only if all ways of logic will finish with some value. If without else will be treated as a statement. And as such will not produce expected behavior, even in the case when if condition is true:
Try this code:
val c1 = if 4>=3 then "bingo!" else "nope!"
println(c1.getClass)
val c2 = if 4>=3 then "bingo!"
println(c2.getClass)
Result:
class java.lang.String
void
I could not find what is referred to.
Scala 3 book is https://docs.scala-lang.org/scala3/book/control-structures.html