Repository: InsertKoinIO/koin
File: koin-projects/koin-core/src/main/kotlin/org/koin/core/scope/ScopeDefinition.kt (Kotlin)

Code before:
            if (beanDefinition.options.override || forceOverride) {
                _definitions.remove(beanDefinition)
            } else {
                val current = definitions.firstOrNull { it == beanDefinition }
                throw DefinitionOverrideException("Definition '$beanDefinition' try to override existing definition. Please use override option or check for definition '$current'")
            }
        }
        _definitions.add(beanDefinition)
    }

    fun remove(beanDefinition: BeanDefinition<*>) {
        _definitions.remove(beanDefinition)
    }

    internal fun size() = definitions.size

    fun <T : Any> saveNewDefinition(
            instance: T,
            clazz: KClass<*> = instance::class,
            qualifier: Qualifier? = null,
            secondaryTypes: List<KClass<*>>? = null,
            override: Boolean = false
    ): BeanDefinition<out Any?> {
        if (!clazz.isInstance(instance)) {
            throw BadDefinitionException("Primary type '$clazz' should be or supertype of '${instance::class}'")
        }
        val found: BeanDefinition<*>? =
                definitions.firstOrNull { def -> def.`is`(clazz, qualifier, this) }
        if (found != null) {
            if (override) {
                remove(found)
            } else {
                throw DefinitionOverrideException("Trying to override existing definition '$found' with new definition typed '$clazz'")
            }
        }
        val beanDefinition = Definitions.createSingle(
                clazz,
                qualifier,
                { instance },
                this,
                Options(isCreatedAtStart = false, override = override),
                secondaryTypes ?: emptyList()
        )
        save(beanDefinition, override)
        return beanDefinition
    }

    fun unloadDefinitions(scopeDefinition: ScopeDefinition) {
        scopeDefinition.definitions.forEach {
            _definitions.remove(it)
        }

Review comment (suggestion):
with reified type we could get rid of that, and avoid to pass a class

Code after:
                _definitions.remove(beanDefinition)
            } else {
                val current = definitions.firstOrNull { it == beanDefinition }
                throw DefinitionOverrideException("Definition '$beanDefinition' try to override existing definition. Please use override option or check for definition '$current'")
            }
        }
        _definitions.add(beanDefinition)
    }

    fun remove(beanDefinition: BeanDefinition<*>) {
        _definitions.remove(beanDefinition)
    }

    internal fun size() = definitions.size

    inline fun <reified T : Any> saveNewDefinition(
            instance: T,
            qualifier: Qualifier? = null,
            secondaryTypes: List<KClass<*>>? = null,
            override: Boolean = false
    ): BeanDefinition<out Any?> {
        val clazz = T::class
        val found: BeanDefinition<*>? =
                definitions.firstOrNull { def -> def.`is`(clazz, qualifier, this) }
        if (found != null) {
            if (override) {
                remove(found)
            } else {
                throw DefinitionOverrideException("Trying to override existing definition '$found' with new definition typed '$clazz'")
            }
        }
        val beanDefinition = Definitions.createSingle(
                clazz,
                qualifier,
                { instance },
                this,
                Options(isCreatedAtStart = false, override = override),
                secondaryTypes ?: emptyList()
        )
        save(beanDefinition, override)
        return beanDefinition
    }

    fun unloadDefinitions(scopeDefinition: ScopeDefinition) {
        scopeDefinition.definitions.forEach {
            _definitions.remove(it)
        }
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true