https://kotlinlang.org logo
#javascript
Title
# javascript
p

PHondogo

11/10/2023, 4:07 PM
Hello! I'm trying to make pluggable modules such that if included as dependency will execute some initialization code specified in init section of companion objects. For JVM target i'm using class scanning approach to make such logic. How to implement it for JS target?
r

Robert Jaros

11/10/2023, 4:21 PM
From my experience you can't. Objects are lazily initialized and no code will be automatically executed just by adding a dependency. Long time ago, with the legacy compiler, I've implemented this in KVision by calling some predefined dynamic functions. But this approach doesn't work with IR. Currently I'm using "module initializers" (objects defined by modules and passed to a function which starts the application - it looks like this:https://kvision.gitbook.io/kvision-guide/1.-getting-started-1/modules).
🙏 1
t

turansky

11/11/2023, 10:09 AM
IR compiler can check dependencies and collect initializers
p

PHondogo

11/11/2023, 10:11 AM
As i know ir compiler can't scan, only get by fqName.
e

Edoardo Luppi

11/11/2023, 9:30 PM
There is an issue in YouTrack for such thing, I'll dig it out tomorrow
But to answer @Robert Jaros, you can do it with
EagerInitialization
IIRC, although it's marked as deprecated. @Ilya Goncharov [JB] might know more about the deprecation status and if we can save the behavior from getting removed.
i

Ilya Goncharov [JB]

11/13/2023, 10:34 AM
This deprecation status is because
EagerInitialization
is not final decision. So in fact it is way to do some initialization step, but we believe there is other way to do it beside making eager initialization of properties. So deprecation is more warning to use it carefully. Of course we will not remove it while we don’t have other ways to support initialization actions of module.
gratitude thank you 2
3 Views