evant / kotlin-inject

Dependency injection lib for kotlin
Apache License 2.0
1.14k stars 51 forks source link

Support arguments for scope annotations #377

Closed vRallev closed 2 months ago

vRallev commented 2 months ago

For each scope we need to define a new scope annotation, e.g.

import me.tatarka.inject.annotations.Scope

@Scope annotation class SingleInAppScope
@Scope annotation class SingleInLoggedInScope
@Scope annotation class SingleInRendererScope
...

Rather, I'd like to define a single annotation and use different arguments:

@Scope
annotation class SingleIn(val scope: KClass<*>)

Then I only need to define new class references and can reuse the same scope:

@SingleIn(AppScope::class)
@SingleIn(LoggedInScope::class)
...

The challenge is that more argument types such as ints, strings and enums must be supported for this.

Note that scope annotations using this mechanism today compile fine, but @SingleIn(AppScope::class) would be treated the same as @SingleIn(LoggedInScope::class). The argument is ignored for comparisons.

The same should be supported for qualifiers #253.

evant commented 2 months ago

Interestingly enough the javadoc for Scope says they should not have attributes, but I verified that dagger does support this.

vRallev commented 2 months ago

Many many years ago (around 2017/18) in a conversation with a Dagger 2 maintainer he mentioned that they have to support it now, because many folks rely on it, but it's not part of the standard. They "accidentally" supported it.