Hey!
Have you ever stumbled upon an idea which requires usage of ServiceLoader in Kotlin Multiplatform?
Me too, twice already.
That's why for the last several evenings I was working on something.
Introducing sweet-spi: Simple SPI (Service Provider Interface) for Kotlin Multiplatform (equivalent of JVM's Service Loader)
https://x.com/why_oleg/status/1822639475736129992
Feel free to check the README for all the details on how to use it and how it works!
👍 5
🚀 2
❤️ 8
e
Edoardo Luppi
08/12/2024, 2:23 PM
For K/JS, have you considered the possibility of allowing JS consumers to contribute services?
For example, I've been working on an internal logging library that allows a VS Code extension consumer to plug-in its own logging implementation.
o
Oleg Yukhnevich
08/12/2024, 2:31 PM
Theoretically it's possible, but not convenient. Even Java interoperability is out of scope for now.
I see it as a Kotlin-only solution for libraries.
Still, all declarations required to implement this on your side are
public
and marked with
InternalSweetSpiApi
annotation - but, they will probably stay forever due compatibility, as they are used by KSP plugin 🙃
Probably you can call
registerInternalServiceModule
(source) somewhere. But you need to be sure, that it's called before any calls to
ServiceLoader.load
Reloading/Unloading of existing instances is not supported now
e
Edoardo Luppi
08/12/2024, 2:34 PM
Thanks! Looks good btw.
Related, I hope
EagerInitialization
gets stabilized at some point, because it's pretty useful.
thank you color 1
o
Oleg Yukhnevich
08/12/2024, 2:34 PM
Just in case: do you have any other use-case except for
ServiceLoader
-like mechanism?
e
Edoardo Luppi
08/12/2024, 2:37 PM
For
EagerInitialization
you mean?
I have quite a lot of top-level properties, and
EagerInitialization
(or globally setting
-Xir-property-lazy-initialization=false
) avoids the lazy initialization checks (
x === VOID
). If the properties are accessed in loops, the check adds unnecessary overhead.