[英]implicit extension methods on type refinements
I seem to be unable to get implicit extension methods to work with type refinements, in scala 2.12.4. 在scala 2.12.4中,我似乎无法获得隐式扩展方法来进行类型细化。 Consider the following:
考虑以下:
trait MyTypeclass[A,B] {
def foo[C](a : Test.Aux[A,B], c : C ) : Test.Aux[A,C]
}
object MyTypeclass {
trait Ops[A,B] {
def instance : MyTypeclass[A,B]
def self : Test.Aux[A,B]
def foo[C](c : C) : Test.Aux[A,C] = instance.foo[C](self,c)
}
object syntax {
def toAllOps[A,B](t : Test.Aux[A,B])(implicit tc : MyTypeclass[A,B]) = new Ops[A,B] {
val instance = tc
val self = t
}
}
}
trait Test[A] {
type B
}
object Test {
type Aux[A,B0] = Test[A] { type B = B0 }
def apply[A,B0] : Aux[A,B0] = new Test[A] { type B = B0 }
implicit def instance[A,B] : MyTypeclass[A,B] = new MyTypeclass[A,B] {
/** Does nothing more than change the second type `B` to the passed value `C` */
def foo[C](a : Test.Aux[A,B], c : C) : Test.Aux[A,C] = new Test[A] { type B = C }
}
import MyTypeclass.syntax.toAllOps
toAllOps(Test[String,Int]).foo(2.0)
//Test[String,Int].foo(2.0) // ERROR : `foo is not a member of Test.Aux[String,Int]`
}
The penultimate line works fine, but the last line doesn't. 倒数第二行工作正常,但最后一行却不行。 The compiler for some reason is unable to link up the typeclass extension methods with the
Aux
. 由于某种原因,编译器无法将typeclass扩展方法与
Aux
链接起来。
Can somebody explain why this is? 有人可以解释为什么吗?
因为toAllOps
不是隐式的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.