Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be able to specify the primary type when declaring instances to a scope #1966

Open
jfontsaballs opened this issue Sep 4, 2024 · 0 comments

Comments

@jfontsaballs
Copy link

Is your feature request related to a problem? Please describe.
I need to declare some instances at scope creation for which I don't know its concrete type until runtime. If I use the declare function as is now, those instances get stored with its base type and then resolution does not work.

Describe the solution you'd like
I'd like for Scope.declare function to be modified so the instance primary type can be specified independently, like this:

 inline fun <reified T> declare(
        instance: T,
        qualifier: Qualifier? = null,
        secondaryTypes: List<KClass<*>> = emptyList(),
        allowOverride: Boolean = true,
        primaryType: KClass<T> = T:class //requesting this parameter be added
    )

Then InstanceRegistry.declareScopedInstance and _createDefinition in BeanDefinition.kt would need to be modified so that they take the primary type as a parameter, but they would not need to be declared as inline.

Describe alternatives you've considered
Right now I'm passing a generic supertype to the classes that need an instance of the problematic class and then I cast it, but it's ugly and dangerous.

Target Koin project
Core

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant