holgerbrandl
12/05/2020, 7:11 PMimport org.koin.core.KoinComponent
import org.koin.core.context.startKoin
import org.koin.dsl.module
class State<T>(val initialValue: T, name: String? = null)
fun main() {
val sim = module(createdAtStart = true) {
single{ State("red") }
single{ State(false) }
}
startKoin() { modules(sim) }
val koin = (object : KoinComponent {}).getKoin()
val boolState = koin.get<State<Boolean>>()
val stringState = koin.get<State<Boolean>>()
}
but this fails with
Exception in thread "main" org.koin.core.error.DefinitionOverrideException: Definition '[Single:'State']' try to override existing definition. Please use override option or check for definition '[Single:'State']'
tynn
12/06/2020, 7:04 AMKClass
as key and thus looses the generic parameter types.tynn
12/06/2020, 7:07 AMtypeOf
to create a custom Qualifier
with this type: TypeQualifier(KType,Qualifier)
holgerbrandl
12/06/2020, 8:34 PMtypeOf
can be used here?tynn
12/06/2020, 9:09 PMdata class TypedQualifier(
private val type: KType,
private val qualifier: Qualifier,
) : Qualifier {
override val value = "$type:${qualifier.value}"
}
Then just override the DSL to support it...
inline fun <reified T> inject2<T>(
qualifier: Qualifier,
...,
) = inject<T>(
TypedQualifier(typeOf<T>(), qualifier),
...,
)
holgerbrandl
12/07/2020, 6:52 AMsingle(TypeQualifier(Boolean::class)){ State<String>("red") }
tynn
12/07/2020, 7:50 AMTypeQualifier
uses KClass<T>
while the implementation above uses KType
which doesn't loose the generic types.deviant
12/07/2020, 1:57 PMholgerbrandl
12/11/2020, 8:36 PM