[英]Dependency Injection in scala
I have a trait 我有个特质
trait Tt[T]{
//tens of methods
}
and 和
class St{ tt: Tt[T] =>
type T = //...
//some methods
}
object St{
def apply[T](tt: Tt[T]) = new St with tt //error
}
The issue is the objects of Tt
are generated by a library. 问题是
Tt
的对象是由库生成的。 Is there a way to avoid implementing all these tens of methods and just "inject the instance"? 有没有一种方法可以避免实施所有这数十种方法,而只是“注入实例”?
I cannot just pass it as a parameter, because of the type variable declaration that is defined inside the class. 由于不能在类中定义类型变量声明,因此我不能仅将其作为参数传递。
Extracting the type variable declaration to the type parameter is not possible. 无法将类型变量声明提取到type参数。
How about class St(val tt: Tt[St#T]) { type T = ... }
关于
class St(val tt: Tt[St#T]) { type T = ... }
There is something else with your apply
though: St
wants the type parameter to Tt
to be the same as it's own type T
(you gotta start using more letters for these types), but apply
is parametrized with T
, which is yet another different type, that just happens to use the same letter. 不过,您的
apply
还有其他内容: St
希望Tt
的类型参数与自己的类型T
(您必须开始为这些类型使用更多字母),但是apply
是用T
参数化的,这是另一种不同的类型,恰好使用相同的字母。 So, def apply[T](tt: Tt[T]) = new St(tt)
won't compile, it needs to be def apply(tt: Tt(St#T)) = new St(tt)
因此,
def apply[T](tt: Tt[T]) = new St(tt)
将不会编译,因此需要def apply(tt: Tt(St#T)) = new St(tt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.