question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bind for generic types

See original GitHub issue

I can’t find a solution to bind generic types and construct instances for them

val kodein by Kodein.lazy {
bind<Generic<*>>() with factory { cls:TypeToken<Generic<*>> -> 
... create instance based on actual * type
}

Onse i trying to get instance with val generic:Generic<String> by instance

got

com.github.salomonbrys.kodein.Kodein$NotFoundException: No provider found for bind<Generic<String>>() with ? { ? }

Registered in Kodein:
        bind<Generic<*>>() with factory { TypeToken<Generic<*>> -> Generic<String> } 

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SalomonBryscommented, May 6, 2018

Kodein 5 offers “subtype factories” and “external source” that both answer your needs.

1reaction
softkotcommented, Sep 22, 2017

I found a workaround. A little bit verbose, but dos not require to iterate all POJO in bind statement.

So first of all create a binder like

Kodein {
Bind<Mapper<*>>(erased()) with multiton { cls: Class<*> -> getMapper(cls) }
}

then create inline extension to KodeinInjected or any other Kodein aware interface

inline fun <reified T> KodeinInjected.mapper() = with(T::class.java).Instance(erased<Mapper<T>>())

and finnaly retrive it with short expressive statement.

val pojo1Mapper by mapper<POJO1>()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I bind a Generic class? - java - Stack Overflow
This is what I tried: bind(new TypeLiteral<AbstractDao<MyDomain>>(){}).to(ConcreteDao.
Read more >
Allow binding generic functions to a given type #37181 - GitHub
So I suggest to provide a syntax that allows to do just that: fix type arguments of generic functions: type Box<T> = ReturnType<typeof...
Read more >
How to: Examine and Instantiate Generic Types with Reflection
Information about generic types is obtained in the same way as ... Call the MakeGenericType method to bind the type arguments to the...
Read more >
Binding generic types using a metatype parameter - Success!
Binding generic types using a metatype parameter about trying to dynamically bind a generic type's parameter to any other type. Well, I finally ......
Read more >
[ninject] Binding open generic types to generic providers?
I sometimes need to bind open generics, which works fine using the syntax: ... What's odd is that if I make the provider...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found