简体   繁体   中英

Is there a compile-time dependency injection tool that supports generics for kotlin (or java)?

All runtime DI (guice, spring, koin, kodein) do support generics.

Dagger 2 - which is only DI compile-time tool that I know - doesn't support generics.

Is there a compile-time dependency injection tool that supports generics for kotlin (or java)?

EDIT: Sample:

class Engine<T>

class Car<T> @Inject constructor(engine: Engine<T>)

If I'll use Dagger 2 and create:

@Module
object MyModule
{
    @Provides
    fun <T> car(engine: Engine<T>): Car<T> = Car(engine)
}

@Component(modules = [MyModule::class])
interface MyComponent
{
    fun <T> car(): Car<T>
}

It will not work because Dagger 2 will at compile time say, that methods with @Provide cannot be of a generic type

According to some code changes in their repository, it seems that kotlin-inject does support generics in general.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM