为什么隐式分辨率不适用于类型预测?

时间:2015-12-03 16:27:59

标签: scala implicit type-level-computation

我正在尝试使用在类型之间建立(部分)顺序的类型类,其中类型通过类型成员知道它们的后继者。但我面临以下隐含的解决问题:

trait T {
  type Next <: T
}

object T {
  implicit def succ[S <: T]: Succ[S,S#Next] = new Succ[S,S#Next]{}
}

trait T1 extends T {
  type Next = T2
}

trait T2 extends T {
  type Next = Nothing
}

trait Succ[X1 <: T, X2 <: T]

object Test {
  implicitly[Succ[T1,T2]](T.succ[T1])  //works

  implicitly[Succ[T1,T2]](T.succ)      //fails
  implicitly[Succ[T1,T2]]              //fails
}

以下是编译器错误:

Error:(23, 29) polymorphic expression cannot be instantiated to expected type;
 found   : [S <: test.objectinf.T]test.objectinf.Succ[S,S#Next]
 required: test.objectinf.Succ[test.objectinf.T1,test.objectinf.T2]
  implicitly[Succ[T1,T2]](T.succ)
                            ^
Error:(23, 29) type mismatch;
 found   : test.objectinf.Succ[S,S#Next]
 required: test.objectinf.Succ[test.objectinf.T1,test.objectinf.T2]
  implicitly[Succ[T1,T2]](T.succ)
                            ^
Error:(24, 13) could not find implicit value for parameter e: test.objectinf.Succ[test.objectinf.T1,test.objectinf.T2]
  implicitly[Succ[T1,T2]]
            ^
Error:(24, 13) not enough arguments for method implicitly: (implicit e: test.objectinf.Succ[test.objectinf.T1,test.objectinf.T2])test.objectinf.Succ[test.objectinf.T1,test.objectinf.T2].
Unspecified value parameter e.
  implicitly[Succ[T1,T2]]
            ^

0 个答案:

没有答案