If expression

less than 1 minute read

One of basic things that gives me a spark in the eye is when I realise in Kotlin that:

Either branch of an if expression can be a multiline block of code surrounded by curly braces.

fun main() {
    val mall = "jem"
    val time = 10

    val isShoppingMallOpen = if (
        mall == "jem" || mall == "vivo"
    ) {
        // in a block
        val openHour = 8
        val closeHour = 12
        println("Mall operates from $openHour to $closeHour")
        time in openHour..closeHour
    } else {
        false // single line
    }

    println("$isShoppingMallOpen")
}

What does Kotlin syntax surprise you? Are you a fan of block code?

Tags:

Categories:

Updated: