is there a way to name case in `when`, so that i c...
# getting-started
i
is there a way to name case in
when
, so that i could write something like this:
Copy code
when (expression generating nullable){
  null -> handle null
  x    -> handle x for which compiler knows its not null anymore
}
j
Extract the expressions on the right into functions with meaningful names
1
Maybe I misunderstood your question, if you want to name
x
you can do so inside the `when`:
Copy code
when(val x = expression)
i
ok, that's what i was wondering, thx
w
Yes, you can:
Copy code
fun maybe(): String? = null

fun asas() {
    when (val maybe = maybe()){
        null -> println("it was null!")
        "something" -> println("maybe is something! Check: $maybe")
        else -> println("I don't know what it is! Check: $maybe")
    }
}
i
thx both! 👍 👍 👍 i much prefer that (being able to write the whole thing as a single expression) than having to first assign value to x and do if/else on it