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

cae*_*eus 1 enums scala dotty gadt scala-3

Scala 3 现在有一种改进的方式来定义 ADT。一种语法糖,消除了用通常的方式做这些事情的所有麻烦sealed trait

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

enum Adt[+A]{
    case Option1
    case Option2
}
Run Code Online (Sandbox Code Playgroud)

在本例中Option1Option2的类型为Adt[Nothing],因为类型参数A是协变的。

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

但如果它是不变的呢?

oow*_*ala 5

在 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
Run Code Online (Sandbox Code Playgroud)