我想采用这种模式:

 def accept[T](a: RList[T]) = true
 def accept[T, V](a: RList[T], b: RList[V])(implicit ev: a.S =:= b.S) = true
 def accept[T, V, Q](a: RList[T], b: RList[V], c: RList[Q])(implicit ev: a.S =:= b.S, ev2: b.S =:= c.S) = true

但让它接受KList,而不是为所有Arial手动覆盖。基本上我想说:“取任意数量的RList成员类型相同的S
RList是一个特征,其中包含类型S。 (有关RList的更多背景以及为什么我这样做,请参见:Constrain function based on origin (Path Dependent type? Type Generation?))

最佳答案

看来您正在做的只是尝试让编译器检查类型是否一致,因为您的方法始终返回true。

您是否可以代替使方法接受任意数量的方法,而接受保证所有S都匹配的“RList列表”?

这样的列表可能是这样构造的:

package rl {

// A simplified version of your RList:
trait RList[T] {
  type S
  def data: List[T]
}

// A list of RLists which have identical S
sealed trait RListList

// RListNil is an empty list
trait RListNil extends RListList {
  def ::[H <: RList[_]](h: H) = rl.::[h.S,H,RListNil](h, this)
}
// there is exactly one RListNil
case object RListNil extends RListNil

// List can be a cons cell of lists sharing the same S
final case class ::[S, H <: RList[_], T <: RListList](head: H, tail: T) extends RListList {

  // We only allow you to cons another to this if we can find evidence that the S matches
  def ::[H2 <: RList[_]](h: H2)(implicit ev: =:=[h.S,S]) = rl.::[S,H2,::[S,H,T]](h, this)
}

现在,如果我们尝试构建一个未满足所有S类型要求的RListList,则编译器将捕获我们:
object RListTest {

  val list1 = new RList[Int] { type S = String; def data = List(1,2,3,4) }
  val list2 = new RList[String] { type S = String; def data = List("1","2","3","4") }
  val list3 = new RList[Double] { type S = Float; def data = List(1.1,2.2,3.3,4.4) }

  val listOfLists1 = list1 :: RListNil // fine
  val listOfLists2 = list2 :: listOfLists1 // still fine, since list1 and list2 have the same S
  val listOfLists3 = list3 :: listOfLists2 // compiler error: Cannot prove that java.lang.String =:= Float

}

这使用的是依赖方法类型,这意味着您需要使用scala 2.10或需要使用2.9.x中的-Ydependent-method-types开关进行编译

09-26 18:21