This message was deleted.
# announcements
s
This message was deleted.
g
It looks that you need
lateinit
☝️ 2
thought it will not be null, it will crash on runtime if it not initialized
d
Yeah I’m not sure why you’re initializing as null. Is this a data class? Can you instantiate it with some not null value ever or can it only ever be “null” ie uninitialized until you initialize it with a not null value? Lateinit seems exactly like what you’d want here without knowing more
If you need to be able to call the method/access the var before it’s initialized and want null to be returned then the signature would be nullable and I don’t think there’s a way to cast that as not null because any call site wouldn’t know if it’s initialized or not. You could use reflection to check if the var is initialized at the call site if you really wanted to though
s
there is also
Delegates.notNull
. It will through an exeption when you try to access it without a non-null value being set to it in the meantime https://stackoverflow.com/questions/44205389/difference-between-delegates-notnull-and-lateinit-kotlin
if that's not exactly what you're looking for you could write your own Delegate, most likely by extending from
ReadWriteDelegate
. https://www.baeldung.com/kotlin/delegated-properties