Koin: Override on the fly declarations (declare)

Created on 31 May 2019  路  3Comments  路  Source: InsertKoinIO/koin

Is your feature request related to a problem? Please describe.

I try to declare a single on the fly. However, I might do that multiple times if the latest value only is to be available to Koin.

When I do the following, I get a org.koin.core.error.DefinitionOverrideException, logically, because the definition already exists.

koin.declare(x)

// Some point later in time, x is updated and we want to replace its value

koin.declare(x) // Exception!

Describe the solution you'd like

I want to override any existing declaration, but there's no such option. This seems easy to implement, because declare under the hood creates a BeanDefinition, which could also use the override option. However, I cannot enable override through declare.

Describe alternatives you've considered

My current alternative is a singleton:

object Holder {
    lateinit var x: Any
}

which I use as a Koin single { Holder.x } and every time I update x I update Holder.x.

Target Koin project

Koin 2.0 in an Android lib.

core accepted feature-proposal

All 3 comments

Yeah, we should allow to set override attribute of the given definition

Hey 馃憢 I'm also interested in this feature. I drafted a proposal here #488. Please let me know what you think @arnaudgiuliani :)

let me check that 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LukasAnda picture LukasAnda  路  3Comments

iRYO400 picture iRYO400  路  3Comments

pchmielowski picture pchmielowski  路  3Comments

haroldadmin picture haroldadmin  路  3Comments

miladsalimiiii picture miladsalimiiii  路  3Comments