macwire icon indicating copy to clipboard operation
macwire copied to clipboard

Support JSR-330

Open backuitist opened this issue 10 years ago • 4 comments

I can see 2 things of interest:

  • @Qualifier - currently implemented with Tagging. I find the JSR meta-annotation to be less intrusive than the Tagging mechanic. 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?

backuitist avatar Aug 26 '15 13:08 backuitist

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).

backuitist avatar Aug 26 '15 13:08 backuitist

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?

adamw avatar Aug 27 '15 10:08 adamw

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

backuitist avatar Aug 27 '15 12:08 backuitist

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)

adamw avatar Aug 31 '15 12:08 adamw