Scala Dotty 联合类型 DaysOfTheWeek 示例

标签 scala dotty

Scala Dotty 官方团队展示了来自 ( https://d-d.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

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

最佳答案

这现在有效:

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

关于Scala Dotty 联合类型 DaysOfTheWeek 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43004353/

相关文章:

scala - 为什么在我使用 com.databricks.spark.avro 时必须添加 org.apache.spark.avro 依赖才能在 Spark2.4 中读/写 avro 文件?

scala - 如何 "hold form submission"并在Play Framework中进行身份验证

scala - 如何在 dotty 中编写带有缩小的 String 类型元组头的匹配类型模式?

scala - Scala 3/Dotty 中的依赖元组

scala - 如何在scala项目中使用dotty?

java - 如何在 Scala 中实例化 Unit?

scala - 未找到请求的操作 : [timestamp <-> java. lang.Long] 的编解码器

scala - Dotty 如何对多态方法进行脱糖?

scala - 在宏扩展时生成函数

scala - Scala 中方法的条件调用