Skip to content

"If as expression" needs some clarification #2312

@ac2epsilon

Description

@ac2epsilon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions