Is there an idiomatic way to memoize values that w...
# multiplatform
m
Is there an idiomatic way to memoize values that works multiplatform (and esp. across threads in Native)? E.g.
private val hash: Int by lazy { … }
but
lazy
doesn’t work across threads in Native afaik.
a
I guess there is no such a way in K/N currently. Mutex requires explicit destroy. I would go with something based on atomics.
s
a
Yep, but I would guess it can call the lambda multiple times under heavy concurrent access. Just like in normal atomics.
m
Thanks, that sounds good! So I basically use
lazy
on JVM/JS and
atomicLazy
on native 🙂 The part “may potentially leak memory” sounds a bit worrying. But I don’t have cyclical references here so it’s probably OK. My operations are idempotent, so it’s okay if the lambda is called multiple times.
a
Then this is your way!