3

The official Scala Dotty team showed this example from (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
}

If I use the latest Dotty nightly build, which as of this post is "0.1.1-20170322-5fd7a95-NIGHTLY", that example results in these errors:

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

Is there any way to get this official example working?

clay
  • 18,138
  • 28
  • 107
  • 192
  • 2
    That talk is one and a half years old and Dotty is still a fast-moving target under heavy development. You might have more luck using an *old* snapshot instead of a *new* one. Or, it might have been part of an idea that was abandoned and never even implemented at all. – Jörg W Mittag Mar 24 '17 at 16:19
  • 2
    Those talk slides are still prominently featured on the current home page of the Dotty project: dotty.epfl.ch. Also, union types are listed as implemented and they work with other examples. – clay Mar 24 '17 at 16:24
  • As far as I understand singleton types are (currently) not allowed in union types, see https://github.com/lampepfl/dotty/issues/1551 – lutzh Mar 25 '17 at 16:06
  • @lutzh I saw that. Can I do the `DaysOfTheWeek` type of enumeration with the updated Dotty that doesn't support unions of singleton types? – clay Mar 25 '17 at 16:55
  • I suggest you to take a look at this talk : https://youtu.be/9lWrt6H6UdE?t=32m58s and check out the enum feature. It seems that it evolved quite a bit. Also, there is a related on the dotty github: https://github.com/lampepfl/dotty/issues/1551 – Louis F. Jun 03 '17 at 02:03

2 Answers2

1

The furthest I got with enums and updated dotty is

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;

}

which still yields the same error but will probably be addressed here : https://github.com/lampepfl/dotty/issues/1551

Louis F.
  • 2,018
  • 19
  • 22
1

This works now:

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
}

Link to Scastie

user
  • 7,435
  • 3
  • 14
  • 44