Hi. Does Koin support conditional creation of bean...
# koin
j
Hi. Does Koin support conditional creation of beans? With condition being depending on another bean’s properties? I.E. so far my module looked like this:
Copy code
data class Config(val fooConfig: String, val barConfig:String)

fun appModule() = module {
    single { ConfigProvider().load() }.bind(Config::class)
    single { Foo(get<Config>().fooConfig) }
    single { Bar(get<Config>().barConfig) }
}
and now I’d like to make
fooConfig
and
barConfig
nullable and create
Foo(…)
and
Bar(…)
only when their configs are not-null. Is there a way to do it?
s
single { get<Config>().barConfig?.let { Bar(it) } }
That should do it, shouldn't it?
j
Tried, but checkModules() complains
s
That sounds a bit like a bug. Seems like checkModules will not take
null
for a valid value, even if the type is nullable. Even just
single<String?> { null }
fails the check. I guess you can get around it by using
Option<Bar>
.
j
Huh. That's a workaround worth considering. We're using arrow anyway, so that shouldn't be a problem... Thanks!