【问题标题】:Type projection alias fails with type bounds类型投影别名因类型边界而失败
【发布时间】:2019-11-18 21:01:22
【问题描述】:

我想要一个使用类型边界和更高种类的类型的类型投影别名,但它意外失败。在 scala 2.12.8 和 2.13.0 中尝试过

以下作品:

object Testing {

  trait One[A] {
    trait Two[B <: A]
  }

  type Test = One[Int]#Two[Int]
}

以下失败:

object Testing {

  trait One[A] {
    type Two[B <: A] = Altogether[A, B]
  }

  trait Altogether[A, B <: A]

  type Test = One[Int]#Two[Int]
}

有错误

type arguments [Int] do not conform to type Two's type parameter bounds [B <: A]

我希望第二个示例也能编译。

以下作品:

type Test = Altogether[Int, Int]

谁能想出一个替代方案?

【问题讨论】:

  • 鉴于错误消息(您应该包括在内),它在我看来肯定是一个错误。如果您从Altogether(但不是从Two)中删除类型绑定,它也会保留。
  • SO 是一个错误的地方报告错误github.com/scala/bug/issues
  • 抱歉,我将更新问题以包含一个实际问题!以及错误信息

标签: scala types


【解决方案1】:

如果您将trait 替换为type,它将按预期工作:

type One[A] = {
    type Two[B <: A] = Altogether[A, B]
}

trait Altogether[A, B <: A]

type Test = One[Int]#Two[Int]

Scala 2.13测试。

【讨论】:

  • 这确实解决了问题中所述的问题,谢谢!。理想情况下,尽管我希望它们成为特征,以便我可以创建它们的实例。我认为您的解决方案不可能吗?很高兴被证明是错误的!
  • @Gesar 正如 Dmytro Mitin 之前所说,它可能是编译器错误,所以它可能会在下一个版本的 Scala 2(2.14?)中得到修复。它已经与 Scala 3 一起编译(但尚未在 2020 年发布)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 2020-10-27
相关资源
最近更新 更多