Pablichjenkov
07/28/2019, 5:42 AMclass SomeClass {
var aVar: OtherClass? = null
fun aFunction() {
aVar?.let {
//doSomething with aVar since 'it' is not null
} ?: run {
//doSomething else when aVar is null
}
}
}
I know that I can do this:
fun aFunction() {
val aVarLocalCopy = aVar
if (aVarLocalCopy != null) {
//doSomething with aVarLocalCopy
} else {
//doSomething else when aVarLocalCopy is null
}
}
But I don’t like the fact of creating one line of code to extract the thread safe local reference copy of aVarProperty. Any pattern or extension function to write above code shorter.Shawn
07/28/2019, 5:54 AMShawn
07/28/2019, 5:54 AMfun aFunction() {
when (val aVarLocalCopy = aVar) {
null -> {
}
else -> {
}
}
}
Shawn
07/28/2019, 5:56 AMaVarProperty
, you’d just be making a new immutable copy of the referencePablichjenkov
07/28/2019, 6:32 AMaVar
is a primitive.
Anyways, back to the original question. When
looks pretty neat, in fact a co-worker had suggested that too, just wanted to see what others are doing. Thanks Shawn