https://kotlinlang.org logo
Title
t

TwoClocks

10/21/2021, 7:59 PM
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

Paul Griffith

10/21/2021, 8:37 PM
the easy option is to use
myThing?.let {}
, since it will capture the definitely-not-null
myThing
into a variable
t

TwoClocks

10/21/2021, 11:17 PM
it's usually the case where I want to set it if it's null, and return it if it's not.
t

thanksforallthefish

10/22/2021, 5:55 AM
why not
var myThing: Any? = null
myThing = myThing ?: something
myThing.doThing() //at least for me compiler is ok with this