codingwell / scala-guice

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

ScalaMapBinder does not support traits as type #50

Open Vadi opened 9 years ago

Vadi commented 9 years ago

Suppose you've a trait that you will implement in child classes -

trait AppMessage {}

Given this injections

    val mapBinder = ScalaMapBinder.newMapBinder[String, AppMessage](binder)
    mapBinder.addBinding("A").toInstance(new AppMessageSampleA)
    mapBinder.addBinding("B").toInstance(new AppMessageSampleB)

and calling them with val map = injector.instance[im.Map[String, AppMessage]] throws not found exception.

Need to support traits

nbauernfeind commented 9 years ago

I don't immediately see why this shouldn't already work. In this case the trait is effectively an interface. Thanks for reporting.

I don't have time to look today, but I will tomorrow.

Daniel-Khodabakhsh commented 2 years ago

I tested this out on version 5.0.2 and it works well with Scala traits now.

The trait I used is one I wrote myself and does not extend any other trait. The inject class also only extends this one trait. Haven't tested with more complex traits.