Reputation: 1246
var something: Something? = null
val notNull: Something = ...
...
fun getSomething() {
something = something ?: notNull
return something
}
Is there in Kotlin any way to do both something = something ?: notNull; return something
at the same time?
Or.. even better, is there a way to do it with generics in an inline function?
Upvotes: 1
Views: 223
Reputation: 9682
fun getSomething(): Something {
return something ?: notNull.also { something = it }
}
// or
fun getSomething(): Something = something ?: notNull.also { something = it }
Upvotes: 3