https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
k

Konstantin Petrukhnov

10/07/2019, 6:28 AM
What is suggested way of creating singletons that will work on ios/jvm?
s

spand

10/07/2019, 6:31 AM
Doesnt this work ?
Copy code
object Bar {
    
}
k

Konstantin Petrukhnov

10/07/2019, 6:34 AM
How do I inject dependencies to it? Separate method?
s

spand

10/07/2019, 6:40 AM
If you are using a dependency framework I assume you should get it from that (I only have experience with Guice). If not then just make a global property and initialize it from the main method/application entry point
k

Konstantin Petrukhnov

10/07/2019, 6:50 AM
It feels that I'm building DI 😄
Thx, object may work with lateinit
m

marstran

10/07/2019, 7:32 AM
Why do you need a singleton for this? Just make a class and give the dependencies through its primary constructor.
👍 1
1
k

Konstantin Petrukhnov

10/07/2019, 11:52 AM
Object doesn't work, as it failed to be mutated. I managed to do it with factory, that provide plain class as singleton.
If there should be only one instance of the object during runtime, singleton sounds suitable.
So class is using primary constructors, and factory handling init and singleton functionality.
In my case, it is a service and could be called first from different classes (or not called at all), so there is need to separate config, init and actual use.
I'll try to publish example this week (if I succeed) 🙂
s

spand

10/07/2019, 12:00 PM
Generally I would say its an anti pattern to have objects exposed as singletons like I think you mean (in the same vein as global variables etc.) We consider singletonness as a scope (in the DI sense) and just inject them like any other dependency.
👍 1
m

marstran

10/07/2019, 12:23 PM
Using a normal class is also better for testing. You can easily mock it for example. Using a singleton
object
would force it to be the same in every environment.
i

ian.shaun.thomas

10/07/2019, 1:49 PM
#kodein and #koin solve this, no?
m

marstran

10/07/2019, 2:20 PM
Those could inject some mock into the object's dependencies, but they can not mock the object itself.
i

ian.shaun.thomas

10/07/2019, 2:23 PM
I was referring to the original question that started this thread. The author even states
It feels that I'm building DI
. Don't do that 😛, we already have great DI options.
m

marstran

10/07/2019, 2:29 PM
Ah, ok.
k

Konstantin Petrukhnov

10/08/2019, 4:39 AM
@ian.shaun.thomas koin doesn't work on ios/native. Kodein requires specific order of initialization.
They maybe good to use in single app, but i don't see (and there is no examples) how they could be used as part of libraries.
@spand you absolutely correct that what i did is not a singleton. My original intention was to use singleton, but because of complex initialization and dependencies, I found other way to do it.
i

ian.shaun.thomas

10/08/2019, 6:17 AM
As part of a library I think you would want to seriously avoid shipping with that kind of dependency however with kodein, and I assume koin, you would simply use them as you would any other dependency such as the jdk and js dependencies commonly used in MPP libs. I would recommend having your core lib and then having another MPP lib that wraps your core lib with the DI choice such that in the future you could theoretically drop it or switch to something else without a lot of hassle.
for kodein, the sample shows using kodein as a common lib, via modules, and then using
application
modules https://github.com/Kodein-Framework/Kodein-Samples/tree/master/di/coffee-maker I would recommend joining the #kodein channel if you have questions as the maintainer is active on here 🙂