I have this kind of pattern when setting optionals...
# getting-started
t
I have this kind of pattern when setting optionals :
if( myThing == null ) { myThing = ... }
I always get an error if I try to reference
myThing
after this block, because the compiler can't tell if it's null or not. I assume this is because threads exist. Is there a option I can give to the compiler to tell it I'm single threaded, and it can ensure the type is correct?
or maybe a
@OptIn
for a file/class/block ?
p
the easy option is to use
myThing?.let {}
, since it will capture the definitely-not-null
myThing
into a variable
t
it's usually the case where I want to set it if it's null, and return it if it's not.
t
why not
Copy code
var myThing: Any? = null
myThing = myThing ?: something
myThing.doThing() //at least for me compiler is ok with this