斯卡拉
Seq
他们没有捕捉到足够的信息来决定他们是否严格。
您可以将要接受的序列列为白名单,如下所示:
import scala.language.higherKinds
// Tag for sequences that are strict.
sealed trait StrictSeq[T[_] <: Seq[_]]
object StrictSeq {
// Evidence for the compiler that lists and vectors are strict.
implicit object ListIsStrict extends StrictSeq[List]
implicit object VectorIsStrict extends StrictSeq[Vector]
}
// Restrict S to be a sequence and to have been tagged as strict.
case class Foo[S[_] <: Seq[_] : StrictSeq](xs: S[Int])
Foo(List(1, 2, 3)) // OK
Foo(Vector(1, 2, 3)) // OK
Foo(Stream(1, 2, 3)) // Compile-time error