繁体   English   中英

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

[英]Type pattern matching and inference error in Scala 3

我正在使用 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

我错过了什么?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM