Scala-在通用接口上使用guice multibinder失败
我有以下界面:Scala-在通用接口上使用guice multibinder失败,scala,playframework,playframework-2.0,guice,Scala,Playframework,Playframework 2.0,Guice,我有以下界面: trait Subject[T] { def fetch() :Future[Option[T]] } 和类别: class ChildSubject@Inject()(dao:Dao) extends Subject[String]{ def fetch(): Future[Option[String]] ={ dao.find("10").map{ name => Some(name) } } 和一个模块: class S
trait Subject[T] {
def fetch() :Future[Option[T]]
}
和类别:
class ChildSubject@Inject()(dao:Dao) extends Subject[String]{
def fetch(): Future[Option[String]] ={
dao.find("10").map{ name => Some(name)
}
}
和一个模块:
class SubjectModule extends AbstractModule with ScalaModule{
override def configure(): Unit = {
val subMulti = ScalaMultibinder.newSetBinder[Subject](binder)
subMulti.addBinding.to[ChildSubject]
}
}
我试着注入这个集合:
@Singleton
class SomeClass @Inject()(subjects: Set[Subject]){
subjects.map{
//do somthing
}
}
我得到以下错误:
play.sbt.PlayExceptions$CompilationException: Compilation error[kinds
of the type arguments (com.test.Subject) do not conform to the expected
kinds of the type parameters (type T).
com.test.Subject's type parameters do not match type T's expected
parameters:
trait Subject has one type parameter, but type T has none]
有什么想法吗??
谢谢 您需要一个
new-TypeLiteral()
用于绑定,它是一个通用类型的接口,如Java Guice中的Subject
。Scala很可能在某种形式上需要相同的功能
类似的方法可能会奏效:
class SubjectModule extends AbstractModule with ScalaModule{
override def configure(): Unit = {
val subMulti = ScalaMultibinder.newSetBinder[Subject[String]](binder)
subMulti.addBinding.to[ChildSubject]
}
}
问题是,我可能有几个不同类型的类,例如:ChildOne扩展Subject[Int],ChildTwo扩展Subject[String],所以我不能将它专门绑定到String。“有道理吗?”托默说。我知道你要去哪里了。您需要multibinder在addBinding步骤中提供类型参数,但是如何提供呢?HmmYou需要能够部分应用类型构造函数。我在哈斯克尔知道这一点。不知道如何在Scala中实现。你希望绑定集有什么类型?@TavianBarnes我希望它被设置为[主题]