https://kotlinlang.org logo
#koin
Title
# koin
j

Jakub Gwóźdź

07/13/2023, 8:53 AM
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

s3rius

07/13/2023, 6:08 PM
single { get<Config>().barConfig?.let { Bar(it) } }
That should do it, shouldn't it?
j

Jakub Gwóźdź

07/13/2023, 6:09 PM
Tried, but checkModules() complains
s

s3rius

07/13/2023, 7:04 PM
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

Jakub Gwóźdź

07/14/2023, 4:52 AM
Huh. That's a workaround worth considering. We're using arrow anyway, so that shouldn't be a problem... Thanks!
27 Views