Is it possible to add a `@Inject` to a typealias? ...
# kotlin-inject
e
Is it possible to add a
@Inject
to a typealias? My use case is where I have a type that I want to use in two different scopes:
Copy code
class Foo

@AppScope
@Inject
typealias AppFoo = Foo

@ScreenScope
@Inject
typealias ScreenFoo = Foo
Currently I have to use provider functions, which is a lot more verbose, especially since I now need a component for those function:
Copy code
@Component
abstract class FooComponent {
  @AppScope @Provides fun provideAppFoo() = AppFoo()
  @ScreenScope @Provides fun provideScreenFoo() = ScreenFoo()
}
(this example is contrived, but I do have an actual use case for something like this)
e
This would only work if you'd want to construct Foo in exactly the same way for both scopes, which seems pretty limited imo. You component sample even implys they are constructed differently with
AppFoo()
and
ScreenFoo()
.
e
Yeah it's a little contrived. The actual use case is related to Android's context, where I want one instance that is a global singleton and uses the application context, and one instance that is scoped to an activity, and uses the activity's context.
Something like:
Copy code
class Foo(val context: Context)

@AppScope
@Inject
typealias AppFoo = Foo

@ScreenScope
@Inject
typealias ScreenFoo = Foo