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

Binding type parameter #8

Closed
nsragow opened this issue Jan 16, 2024 · 4 comments
Closed

Binding type parameter #8

nsragow opened this issue Jan 16, 2024 · 4 comments

Comments

@nsragow
Copy link

nsragow commented Jan 16, 2024

I can't figure out a way to bind a type parameter:

class Container<Type>(val obj: Type) {
    fun typeBinder(builder: Kodein.Builder) {
        builder.apply { 
            bind() from singleton { obj }
        }
    }
}

Is this possible?

@nsragow
Copy link
Author

nsragow commented Jan 16, 2024

I came up with a workaround:

class Container<Type : Any>(val obj: Type, val klass: KClass<Type>) {
    fun typeBinder(builder: Kodein.Builder) {
        builder.apply {
            val s = Singleton(
                scope,
                contextType,
                TT(klass),
                null,
                true,
            ) {
                obj
            }
            bind() from s
        }
    }
}

Not sure if this is how I am supposed to be using this though.

@romainbsl
Copy link
Member

Hi, what are you trying to bind really ? (I guess that your are working with Kodein (you are on Kaverit repo here).

Kodein should have everything you need. If you are trying to bind an existing object, you can do:

class Foo

val obj = Foo()

val di = DI {
 bindInstance { obj }
}

val injectedFoo : Foo by di.instance()

assert(obj == injectedFoo)

@nsragow
Copy link
Author

nsragow commented Jan 22, 2024

O sorry my bad, I'll post to the Kodein repo

@romainbsl
Copy link
Member

duplicate: kosi-libs/Kodein#449

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

2 participants