macwire
macwire copied to clipboard
Support JSR-330
I can see 2 things of interest:
@Qualifier- currently implemented withTagging. I find the JSR meta-annotation to be less intrusive than theTaggingmechanic. That alone has an added value IMO.Provider- currently implemented with call-by-name.
Although these features wouldn't be immensely useful as they already have scala-ish equivalents, they could be useful when integrating with java libraries.
What do you think? Would it be useful? doable?
Note about runtime dependencies: let's suppose that we move Tagging away from the macros (#49), and you decide to go for the JSR-330 annotation style, then you could just limit your runtime classpath to javax.inject.jar (2KB).
Tagging has the advantage over @Qualifier as it creates a *distinct type`. That is, you cannot use an instance tagged with the incorrect tag because of Scala's typing rules. I'd like to keep the macro a replacement for hand-writing the constructor calls, where the manual process of finding an instance is in the same way unambiguous as the automatic one. With annotations, you could end up with a couple of instances eligible for using at a given place (using normal typing rules).
Not sure about provider - do you mean using defs for defining dependencies?
Tagging has the advantage over @Qualifier as it creates a distinct type
That's also a disadvantage: it makes your client code more cumbersome:
// that's a stupid example, but you get the idea
class Person(firstName: String @@ FirstName, lastName: String @@ LastName)
// scala
new Person("john".taggedWith[FirstName], "doe".taggedWith[LastName])
// java
// ???
As opposed to
class Person(@FirstName firstName: String, @LastName lastName: String)
// scala & java
new Person("john", "doe")
Again, it's just a matter of giving choice and widening integration possibilities.
Not sure about provider
Take a look at these:
- https://github.com/google/guice/wiki/ProviderBindings
- https://github.com/google/guice/wiki/InjectingProviders
As for @Qualifier, while I prefer to keep things type-safe and wouldn't use this approach myself, I don't see a reason why macwire couldn't special-case such annotations and use them to narrow down the values to be used for a specific parameter. I don't think a new dependency would be needed, just checking the fully qualified name of the annotation as a string should be enough.
Providers - well these are dependencies defined using a def instaead of a val/lazy val, no? (I don't see what a def can't do what a provider can)