Skip to content

Commit

Permalink
More ADT material
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky authored and felixmulder committed May 26, 2017
1 parent 5244d76 commit def3bcf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/docs/reference/adts.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,44 @@ val res3: t2.Option.Some[Int] = Some(2)
scala> scala> new Option.Some(2)
```

As all other enums, ADTs can have methods on both the enum class and
its companion object. For instance, here is `Option` again, with an
`isDefined` method and an `Option(...)` constructor.

```scala
enum class Option[+T] {
def isDefined: Boolean
}
object Option {
def apply[T >: Null](x: T): Option[T] =
if (x == null) None else Some(x)

case Some[+T](x: T) {
def isDefined = true
}
case None {
def isDefined = false
}
}
```

Enumerations and ADTs have been presented as two different
concepts. But since they share the same syntactic construct, they can
be seen simply as two ends of a spectrum and it is perfectly possible
to conctruct hybrids. For instance, the code below gives an
implementation of `Color` either with three enum values or with a
parameterized case that takes an RGB value.

```scala
enum Color(val rgb: Int) {
case Red extends Color(0xFF0000)
case Green extends Color(0x00FF00)
case Blue extends Color(0x0000FF)
case Mix(mix: Int) extends Color(mix)
}
```





Expand Down
18 changes: 18 additions & 0 deletions tests/pos/reference/adts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,21 @@ enum Color(val rgb: Int) {
case Mix(mix: Int) extends Color(mix)
}

object t3 {

enum class Option[+T] {
def isDefined: Boolean
}
object Option {
def apply[T >: Null](x: T): Option[T] =
if (x == null) None else Some(x)

case Some[+T](x: T) {
def isDefined = true
}
case None {
def isDefined = false
}
}

}

0 comments on commit def3bcf

Please sign in to comment.