3

官方 Scala Dotty 团队展示了来自 ( https://dd.me/talks/scalaworld2015/#/12 )的这个例子

object DaysOfTheWeek {
  object Mon
  object Tue
  object Wed
  object Thu
  object Fri
  object Sat
  object Sun

  type Weekend = Sat.type | Sun.type
  type Workweek = Mon.type | Tue.type | Wed.type | Thu.type | Fri.type
  type All = Weekend | Workweek
}

如果我使用最新的 Dotty nightly build,截至本文为“0.1.1-20170322-5fd7a95-NIGHTLY”,该示例会导致以下错误:

Error:(13, 18) Singleton type DaysOfTheWeek.Sat.type is not allowed in a union type
Error:(13, 29) Singleton type DaysOfTheWeek.Sun.type is not allowed in a union type
Error:(14, 19) Singleton type DaysOfTheWeek.Mon.type is not allowed in a union type
Error:(14, 30) Singleton type DaysOfTheWeek.Tue.type is not allowed in a union type
Error:(14, 41) Singleton type DaysOfTheWeek.Wed.type is not allowed in a union type
Error:(14, 52) Singleton type DaysOfTheWeek.Thu.type is not allowed in a union type
Error:(14, 63) Singleton type DaysOfTheWeek.Fri.type is not allowed in a union type

有什么办法可以让这个官方示例正常工作吗?

4

2 回答 2

1

我用枚举和更新的 dotty 得到的最远的是

enum class DaysOfTheWeek
object DaysOfTheWeek {
case Mon
case Tue
case Wed
case Thu
case Fri
case Sat
case Sun;

  type Weekend = Sat.type | Sun.type;
  type Workweek = Mon.type | Tue.type | Wed.type | Thu.type | Fri.type;
  type All = Weekend.type | Workweek.type;

}

仍然会产生相同的错误,但可能会在这里解决:https ://github.com/lampepfl/dotty/issues/1551

于 2017-06-03T02:15:48.563 回答
1

这现在有效:

enum DaysOfTheWeek {
case Mon
case Tue
case Wed
case Thu
case Fri
case Sat
case Sun
  type Weekend = Sat.type | Sun.type
  type Workweek = Mon.type | Tue.type | Wed.type | Thu.type | Fri.type
  type All = Weekend | Workweek
}

链接到Scastie

于 2020-05-06T16:01:55.583 回答