Scala3中的类型模式匹配和推理错误



我在Scala 3中玩类型类,遇到了一个无法解释的编译错误。

考虑以下代码:

trait Transformation[Input, Output <: Tuple]:
def apply(x: Input): Output

trait ListOfTransformations[T[_, _] <: Transformation[_, _], Input <: Tuple, Output <: Tuple] extends Transformation[Input, Output]
object ListOfTransformations:
given empty[T[_, _] <: Transformation[_, _]]: ListOfTransformations[T, EmptyTuple, EmptyTuple] with
def apply(t: EmptyTuple): EmptyTuple = t
given nonEmpty[T[_, _] <: Transformation[_, _], Head, Tail <: Tuple, HeadOutput <: Tuple, TailOutput <: Tuple](
using
ht: T[Head, HeadOutput],
tt: ListOfTransformations[T, Tail, TailOutput]
): Transformation[Head *: Tail, Tuple.Concat[HeadOutput, TailOutput]] with
def apply(x: Head *: Tail): Tuple.Concat[HeadOutput, TailOutput] = ht(x.head) ++ tt(x.tail)

我得到:

Found:    Tuple.Head[Head² *: Tail]
Required: nonEmpty.this.ht.Input
where:    Head  is a type in object Tuple which is an alias of [X <: NonEmptyTuple] =>> 
X match {
case [x, _ <: Tuple] =>> scala.runtime.MatchCase[x *: _, x]
}
Head² is a type in class nonEmpty
Tail  is a type in class nonEmpty with bounds <: Tuple

我错过了什么?

将带边界的类型构造函数用作类型参数时,请确保使用实际参数而不是通配符。使用T[a, b] <: Transformation[a, b]而不是T[_, _] <: Transformation[_, _]可以进行编译(Scastie(。前者采用一个类型构造函数,当给定两个类型时,该构造函数为一些我们不知道的ab提供一个类型,该类型是Transformation[a, b]的子类型。通过不使用通配符(并忽略T的实际参数(,您可以让编译器准确地知道T[a, b]是其子类型。

最新更新