https://kotlinlang.org logo
#announcements
Title
# announcements
n

Nikky

08/30/2019, 7:50 PM
This property is marked as @Transient and therefore must have an initializing expression
seems like this is no longer valid?
Copy code
@Transient
lateinit var rootDir: File
this seems to only be a error on 1.3.50, worked fine on 1.3.41
c

Casey Brooks

08/30/2019, 8:01 PM
if it's not nullable or has a default value, what would you expect it to be after deserialization? Can't be null, it's a non-nullable value.
lateinit
doesn't make sense because it's in an undefined state after deserialization, it's never initialized. There's no way for the deserializer to infer a default value for every possible type, so the only reasonable solution is to require the developer to provide it
n

Nikky

08/30/2019, 8:05 PM
well exactly why it is lateinit, i set it manually, i guess i might have to only use nullables from now on
c

Casey Brooks

08/30/2019, 8:11 PM
lateinit
is designed for something you know is going to be set. If you can't reasonably expect a property to actually be set during some initialization process, you should probably keep it nullable. Keep in mind that
lateinit
does not mean "null until set", as would be the case for such variables in Java; it's a much stronger contract than that
n

Nikky

08/30/2019, 8:13 PM
well it worked in 1.3.41 with older serialization, this seems like a oversight that maybe was never tested and .. thats exactly how i am using it.. it is set first thing after the deserialization and it would blow up horribly without that being initialized
other properties look for example like so:
Copy code
@Transient
val sourceFolder: File
    get() = rootDir.resolve(sourceDir)
these do not error at all, like you would expect them to i guess i would hate to cover everything with safecalls and nullchecks if lateinit is exactly what i need
s

Stephan Schroeder

09/02/2019, 9:38 AM
basically
lateinit
is for dependency injection into property (into constructor works obviously without it)
3 Views