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

rocketraman

12/10/2020, 4:14 PM
I have a lazy transient property in a
<http://java.io|java.io>.Serializable
class. However accessing
foo
from
@delegate:Transient val foo: Foo by lazy { }
causes a NPE on deserialization. How can I create a lazy property that the VM does not try to serialize, but at the same time does the right thing?
r

Ryan

12/10/2020, 4:18 PM
Don’t use lazy or Don’t use seriazable?
r

rocketraman

12/10/2020, 4:19 PM
No choice on the
Serializable
as its inherited from Apache Beam (and it uses it). Not using
lazy
is an option, but first I wanted to see if there was a way to make it work.
g

gildor

12/10/2020, 4:22 PM
You probably should also annotate getter with Transient, @get:Transient
r

rocketraman

12/10/2020, 4:22 PM
Tried that, results in a compile error
g

gildor

12/10/2020, 4:23 PM
What kind error?
r

rocketraman

12/10/2020, 4:23 PM
This annotation is not applicable to target 'member property with delegate' and use site target '@get'
r

Ryan

12/10/2020, 4:23 PM
if i had to guess the private nullable variable isn’t getting those annots propegated to it
i’ve stopped using lazy due to all the hiccups
r

rocketraman

12/10/2020, 4:44 PM
I've worked around it by storing an `@Transient var`(non-lazy), and then re-setting it in
readObject
. There must be a more elegant approach though.
5 Views