Rob Elliot
09/02/2022, 8:18 AMinit block entirely lazy? Example in the thread.
(I suspect I'm going to get "smells of a poor design" and that may well be right but my imagination is failing me on how to fix it at the moment...)Rob Elliot
09/02/2022, 8:21 AMinterface Database {
fun queryByName(s: String): String
fun link(thing1: String, thing2: String)
}
class SingletonFoo(
database: Database
) {
val lookedUpFromDb1: String = database.queryByName("foo")
val lookedUpFromDb2: String = database.queryByName("bar")
init {
database.link(lookedUpFromDb1, lookedUpFromDb2)
}
}Rob Elliot
09/02/2022, 8:22 AMSingletonFoo on app start - which means that the database needs to be accessible when the app starts, and I don't want to couple the app and the database in that way.
I can't just make lookedUpFromDb1 and lookedUpFromDb2 lazy because I refer to them in the init block.Rob Elliot
09/02/2022, 8:24 AMinterface SingletonFoo {
val lookedUpFromDb1: String
val lookedUpFromDb2: String
}
class ActiveSingletonFoo(
database: Database
) : SingletonFoo {
override val lookedUpFromDb1: String = database.queryByName("foo")
override val lookedUpFromDb2: String = database.queryByName("bar")
init {
database.link(lookedUpFromDb1, lookedUpFromDb2)
}
}
class LazySingletonFoo(
database: Database
) : SingletonFoo {
private val activeSingletonFoo by lazy { ActiveSingletonFoo(database) }
override val lookedUpFromDb1: String by lazy { activeSingletonFoo.lookedUpFromDb1 }
override val lookedUpFromDb2: String by lazy { activeSingletonFoo.lookedUpFromDb2 }
}Rob Elliot
09/02/2022, 8:27 AMActive and once in the Lazy type seems like a lot of boilerplate. But I don't think there's a way to do
class LazySingletonFoo(...) : SingletonFoo by delegate where the delegate is initialised lazily.Rob Elliot
09/02/2022, 8:42 AMinit block goes away and the properties on SingletonFoo can just be lazy without needing an interface.
Still, be interesting to know if this is possible in any way.