Skip to content

bind[F[T]], where T is a value type (long, int, etc), does not work with @Inject constructors #56

@woparry

Description

@woparry

example:

scala> val module = new AbstractModule with ScalaModule {
     |   def configure() {
     |     bind[Option[Int]].toInstance(Some(1))
     |   }
     | }
module: com.google.inject.AbstractModule with net.codingwell.scalaguice.ScalaModule = $anon$1@4bd5a2cc

scala> class Test @Inject()(val optionOfInt: Option[Int])
defined class Test

scala> Guice.createInjector(module).getInstance(classOf[Test])
com.google.inject.ConfigurationException: Guice configuration errors:

1) No implementation for scala.Option<java.lang.Object> was bound.
  while locating scala.Option<java.lang.Object>
    for parameter 0 at Test.<init>(<console>:10)
  while locating Test

1 error
  at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1042)
  at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1001)
  at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051)
  ... 43 elided

Note that it works fine if you use @provides instead

scala> val module = new AbstractModule with ScalaModule {
     |   @Provides
     |   def providesOptionOfInt: Option[Int] = Some(1)
     |   def configure = {}
     | }
module: com.google.inject.AbstractModule with net.codingwell.scalaguice.ScalaModule{def providesOptionOfInt: Option[Int]} = $anon$1@7ba2e475

scala> class Test @Inject()(val optionOfInt: Option[Int])
defined class Test

scala> Guice.createInjector(module).getInstance(classOf[Test])
res0: Test = Test@10042b25

or if T is a reference type

scala> val module = new AbstractModule with ScalaModule {
     |   def configure {
     |     bind[Option[String]].toInstance(Some("fish"))
     |   }
     | }
module: com.google.inject.AbstractModule with net.codingwell.scalaguice.ScalaModule = $anon$1@3ea66c1d

scala> class Test @Inject()(val optionOfString: Option[String])
defined class Test

scala> Guice.createInjector(module).getInstance(classOf[Test])
res1: Test = Test@22e84b6e

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions