With Dagger you can add qualifier (annotations) to...
# language-proposals
e
With Dagger you can add qualifier (annotations) to generic classes like
Copy code
@Named("coins") Safe coinSafe;
@Named("jewelry") Safe jewelrySafe;
In a large codebase people might forget to add the right annotation or in the case of
Named
, the correct value. Being able to create a
typealias
like below would be really helpful.
Copy code
typealias CoinSafe = @Named("coins") Safe
typealias JewelrySafe = @Named("jewelry") Safe

// and usage becomes
CoinSafe coinSafe;
JewelrySafe jewelrySafe;
I’m also quite skeptical though, because
typealias
(right now) implies interchangeability and for special cases/mistakes with this feature, interchanging types might introduce bugs.
1
2
b
How would this work?
Copy code
typealias CoinSafe = @Named("coins") Safe
typealias What = @Named("what") CoinSafe
g
Yes, it’s not clear how this should work, annotations are context specific things, you can annotate class, constructor argument, field etc. In your example you actually annotate type, not sure how this can help with dagger. And it is not how type aliases work. You actually don’t need annotation in this case. Kotlin metadata contains info about typealias, so can be used by annotation processor, but this should be implemented on the side of dagger compiler, rather than special language feature. It would be even more useful with upcoming inline classes. There is an issue on dagger repo with this and other Kotlin specific proposals, but all of them require Kotlin specific parts in dagger compiler https://github.com/google/dagger/issues/900
1
e
Yes you’re probably right @gildor. I also think inline classes will do exactly what I want.
g
Problem, that dagger cannot handle inline classes properly, dagger compiler should use Kotlin metadata to understand real class type, otherwise if you use standard Java APT you will see only type of value, not a type of inline class wrapper
same problem as with nullable, dagger don’t see difference between
String
and
String?