docs.scala-lang icon indicating copy to clipboard operation
docs.scala-lang copied to clipboard

"If as expression" needs some clarification

Open ac2epsilon opened this issue 3 years ago • 1 comments

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

ac2epsilon avatar Feb 04 '22 20:02 ac2epsilon

I could not find what is referred to.

Scala 3 book is https://docs.scala-lang.org/scala3/book/control-structures.html

som-snytt avatar Jun 12 '22 09:06 som-snytt