Is there any way to initialize file-scoped variabl...
# javascript
m
Is there any way to initialize file-scoped variables lazily just like on JVM without adding
lazy
& related overhead to every single one of them? In my project I use the pattern
val foo = object : SomeBaseClass { … }
quite a lot and just now recognized that there’s an insane amount of initialization going on during startup. All such
val
get evaluated at startup and all properties of these objects get evaluated too which in turn runs all their delegate providers.
Oh, I’ve just found this one: https://youtrack.jetbrains.com/issue/KT-43222
s
Its coming to IR under a flag.
🎉 5
m
Awesome, thanks. Is the flag an intermediary solution and it may become standard?
s
I hope it would become standard.
👍 1
BTW, if you replace
val foo = object : SomeBaseClass { … }
with
object Foo : SomeBaseClass { … }
, it would be initialized lazily.
m
Yes. But I use the former pattern a lot for
private val
. I cannot easily do that with named objects because even
private object
causes name collisions across files 😞 I’d have to unnecessarily come up with a crazy amount of named objects to just work around that. And all places that use properties of these objects would also have to use these class names. I really miss namespaces sometimes 🤷‍♂️
1
s
@Marc Knaup interesting, could you file an YT ticket for the private object name collision issue? I couldn’t find anything like that filed. I think we can remove most non-
@JsExport
name collision errors in IR compiler in the future.
s
I reported the same 4 years ago on the JVM: https://youtrack.jetbrains.com/issue/KT-17699