我在理解以下现象时遇到了一些麻烦:
I'm having a little trouble understanding the following phenomenon:
trait Trait[A] { def traitType: String } object Trait { implicit val stringTrait: Trait[String] = new Trait[String] { def traitType: String = "string" } implicit val intTrait: Trait[Int] = new Trait[Int] { def traitType: String = "int" } } class Media[A] { // works def mediaType(implicit t: Trait[A]): String = t.traitType // does not compile def mediaType: String = implicitly[Trait[A]].traitType } object Main { def main(args: Array[String]) { val a = new Media[String] val b = new Media[Int] println(a.mediaType) println(b.mediaType) } }在上面的代码段中,我展示了 mediaType 方法的2种不同实现(在编译代码时,我将其中之一注释掉了).但是,使用隐式的版本无法编译?我收到以下错误消息:
In the above snippet I show 2 different implementations of the mediaType method (I comment one of them out when compiling the code). However the version using implicitly does not compile? I get the following error message:
impl.scala:19: error: could not find implicit value for parameter e: Trait[A] def mediaType: String = implicitly[Trait[A]].traitType ^ one error found我确实知道没有可用的Trait [A]隐式值.我不明白为什么A不能解析为实例化Media的类型.我想我在这里对C ++模板的思考太多了,如果有人可以向我指出正确的方向,我将不胜感激.
I do understand that there is no implicit value of Trait[A] available. I don't understand why A does not get resolved to the type Media gets instantiated with. I think I'm thinking too much in terms of C++ templates here and I would be very grateful if someone could give me a pointer into the right direction.
关于, raichoo
Regards, raichoo
推荐答案编译器需要证据,证明A存在隐式Trait实例.在第一个mediaType实现中,您声明此要求.但是在第二种实现中,从编译器的角度来看,没有这样的保证.因此,为了使其工作,您应该要求Media类的用户提供它.您可以通过上下文绑定来做到这一点:
Compiler needs evidence, that there exist implicit Trait instance for the A. In first mediaType implementation you declare this requirement. But in the second implementation, from the compiler's point of view, there is no such guarantee. So in order to make it work you should ask users of Media class to provide it. You can make this with context bound:
class Media[A : Trait] { def mediaType: String = implicitly[Trait[A]].traitType }这也可以写得更明确:
class Media[A](implicit val evidence: Trait[A]) { def mediaType: String = implicitly[Trait[A]].traitType }因此换句话说,默认构造函数需要隐式evidence,并且如果不提供(显式或隐式)类,用户将无法实例化Media类.
So in other words default constructor requires implicit evidence and users would not be able to instantiate Media class without providing it (explicitly or implicitly).
更多推荐
Scala:“隐式"和类型参数
发布评论