Scala 类型细化的隐式扩展方法
在scala 2.12.4中,我似乎无法让隐式扩展方法处理类型优化。考虑以下事项:Scala 类型细化的隐式扩展方法,scala,typeclass,implicit,refinement-type,Scala,Typeclass,Implicit,Refinement Type,在scala 2.12.4中,我似乎无法让隐式扩展方法处理类型优化。考虑以下事项: 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 :
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]`
}
倒数第二行可以,但最后一行不行。由于某种原因,编译器无法将typeclass扩展方法与Aux
链接起来
有人能解释一下为什么会这样吗?因为
toAllOps
不是隐式的。因为toAllOps
不是隐式的。谢谢,我担心我将不得不删除这个问题,因为我无忧无虑。。。哼!如果投票人也以“离题->琐碎的印刷错误”的理由投票结束投票,那就不会太糟糕了;顺便说一句:还有(出于某种原因,它突然出现在我的脑海中:P)谢谢,我担心由于我的无忧无虑,我将不得不删除这个问题。。。哼!如果投票人也以“离题->琐碎的印刷错误”的理由投票结束投票,那就不会太糟糕了;旁白:还有(出于某种原因,它突然出现在我的脑海中:P)