codingwell / scala-guice

Scala extensions for Google Guice
Apache License 2.0
341 stars 44 forks source link

Binding Providers of parameterised types #60

Open gravelld opened 7 years ago

gravelld commented 7 years ago

Consider:

private class AnOptionProvider @Inject()(s:String) extends Provider[Option[String]] {
  def get = Option(s)
}

I can do:

bind(new TypeLiteral[Option[String]]() {}).toProvider(classOf[AnOptionProvider])

But I can't do:

bind(new TypeLiteral[Option[String]]() {}).toProvider[AnOptionProvider]

Which is slightly more elegant.

I get the feeling I'm missing something from the README when it says:

bind[A].toProvider[BProvider]
bind[A].toProvider[TypeProvider[B]]

Assuming this solved it, could that example be fleshed out?

tsuckow commented 7 years ago

The issue is that bind(new TypeLiteral[Option[String]]() {}) is invoking the guice native method and thus not returning the scala wrapped builders that support the .toProvider[AnOptionProvider] syntax.

This does work:

 bind[Option[String]].toProvider[AnOptionProvider]

I'm looking into what it would take to override the methods and return the Scala versions.

tsuckow commented 7 years ago

This is an issue I have tried to resolve before, unfortunately at every turn I end up with an ambiguous method error.