J
J
JZX2021-08-13 19:02:22
Kotlin
JZX, 2021-08-13 19:02:22

How to test a variable for null and assign a value to it at the same time?

Are there other options besides if, let, apply?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vasily Bannikov, 2021-08-13
@JZX

Seems you need this:
variable = variable ?: value
https://stackoverflow.com/a/44316114/14163502

D
Denis Zagaevsky, 2021-08-13
@zagayevskiy

It cannot be done at the same time. Why you asking?

M
MinTnt, 2021-08-14
@MinTnt

Yes, there are different ones, for example. I'll probably add more

val a = null
println(when (a) {
    null -> "it's null"
    else -> "it's not null"
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question