thought it will not be null, it will crash on runtime if it not initialized
d
Dave K
07/05/2021, 3:57 AM
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
Dave K
07/05/2021, 4:01 AM
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