We've got some code which contains mutable referen...
# koin
r
We've got some code which contains mutable references, so we gotta create a new object on each invocation. Currently said object is created by Koin via regular
get()
, but then the injected code reuses the implementation. Can I somehow forward the injection to the actual call site, so an object is being created on each invocation?
s
You can perhaps pass a provider?
Copy code
class YourClass(
  provider: () -> YourDependency
) {
  fun foo() {
    val newInstance = provider()
  }
}

// Koin
factory {
  YourDependency()
}

factory {
  YourClass(
    provider = { get() }
  )
}
r
That's what did pretty much, after finding out the hard way Koin (probably because of Jvm limitations) can't distinguish between
() -> A
and
() -> B
factories
s
Would it be too much overhead to create interfaces
Copy code
fun interface ProviderA : () -> A
fun interface ProviderB : () -> B
To help it with types?
r
Oh, that's a bit neater
s
Or even putting type explicitly should do as well:
Copy code
factory {
  YourClass(
    provider = { get<YourDependency>() }
  )
}