https://kotlinlang.org logo
Title
r

RedCross

06/03/2019, 12:11 PM
The pattern Singleton in Kotlin is designed by object, so if in java to get an instance of the singleton while we create it : public static SingletonClass getInstance(param1, param2, ...) which will initialize some var "Dynamicly" in the SinletonClass and in the same time create the instance using this params, and as we know there is no constructor in kotlin objects, so how do i pass my params to the object dynamicly ?
g

gildor

06/03/2019, 12:16 PM
You cannot
But you can implement this the same way as in Java
It was discussed already a couple times
m

Marc Knaup

06/03/2019, 12:17 PM
You can't. If you really want an
object
then you could make it wrap a regular
class
and add something like
fun initialize(…)
to your
object
which sets up the wrapped
class
instance. Anytime you access the
object
you throw an exception if
initialize
wasn't called yet.
g

gildor

06/03/2019, 12:20 PM
also a side note, I believe this is a very bad anti-pattern
☝️ 7