Scala 3 中枚举的不变默认类型

Invariant default type for enums in Scala 3

Scala 3 现在改进了定义 ADT 的方法。 一种语法糖,可以消除使用通常的 sealed trait 方式进行操作的所有麻烦。

所以我会用一个例子来解释我的问题

enum Adt[+A]{
    case Option1
    case Option2
}

在这种情况下 Option1Option2 是类型 Adt[Nothing],因为类型参数 A 是协变的。

如果枚举是逆变的,它们的类型将是 Adt[Any]

但是如果它是不变的呢?

在 Dotty 0.27.0-RC1 中,这是一个错误:


scala> enum Adt[A]{
     |     case Option1
     |     case Option2
     | }
2 |    case Option1
  |    ^^^^^^^^^^^^
  |    cannot determine type argument for enum parent class Adt,
  |    type parameter type A is non variant
3 |    case Option2
  |    ^^^^^^^^^^^^
  |    cannot determine type argument for enum parent class Adt,
  |    type parameter type A is non variant