Skip to content

Commit

Permalink
Switch to Scala 3 enum style
Browse files Browse the repository at this point in the history
  • Loading branch information
lukestephenson-zendesk committed Oct 6, 2023
1 parent dd1fffa commit 2d4ebdf
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 34 deletions.
10 changes: 4 additions & 6 deletions cheat-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,14 @@ The values of a product type typically contain several values, called fields. Fo
Values of algebraic types are analysed with pattern matching, which identifies a value by its constructor or field names and extracts the data it contains.

### Sealed trait
When defining an algebraic data type using sealed traits, it allows the compiler to exhaustively check the possible cases in match expressions.
When defining an algebraic data type using enums, it allows the compiler to exhaustively check the possible cases in match expressions.
The compiler will emit a warning (or an error is the option "-Xfatal-warnings" option is enabled) if you have missed a specific case.
The compiler knows all of the subtypes of the trait that can possibly exist as they can only be extended in the file.

```scala
sealed trait MyBooleanType

case object True extends MyBooleanType

case object False extends MyBooleanType
enum MyBooleanType {
case True, False
}
```

### Pattern matching
Expand Down
9 changes: 8 additions & 1 deletion src/main/scala/introcourse/level02/TypesExercises.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,24 @@ object TypesExercises {
*
* You can read the following as a `TrafficLight` is either `Red` or `Yellow` or `Green`.
*
* A sealed trait can only be extended in the same file that it is defined.
* A sealed trait / enum can only be extended in the same file that it is defined.
*
* This technique helps you make invalid states/values irrepresentable in your programs
*/
enum TrafficLight {
case Red, Yellow, Green
}

// Scala 2.12 equivalent syntax
/*
sealed trait TrafficLight
case object Red extends TrafficLight
case object Yellow extends TrafficLight
case object Green extends TrafficLight
*/

/**
* scala> showTrafficLight(Red)
Expand Down
10 changes: 3 additions & 7 deletions src/main/scala/introcourse/level03/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ package object level03 {

case class Person(name: String, age: Int)

sealed trait TrafficLight

case object Red extends TrafficLight

case object Yellow extends TrafficLight

case object Green extends TrafficLight
enum TrafficLight {
case Red, Yellow, Green
}

}
10 changes: 3 additions & 7 deletions src/main/scala/introcourse/level04/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ package object level04 {

case class Person(name: String, age: Int)

sealed trait TrafficLight

case object Red extends TrafficLight

case object Yellow extends TrafficLight

case object Green extends TrafficLight
enum TrafficLight {
case Red, Yellow, Green
}

}
12 changes: 5 additions & 7 deletions src/main/scala/introcourse/level05/EitherExercises.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import ExceptionExercises.personStringPairs
object EitherExercises {

//ADT for representing errors as values
sealed trait AppError

case object EmptyName extends AppError

case class InvalidAgeValue(value: String) extends AppError

case class InvalidAgeRange(age: Int) extends AppError
enum AppError {
case EmptyName
case InvalidAgeValue(value: String)
case InvalidAgeRange(age: Int)
}

/**
* In the ExceptionExercises exercise we used Exceptions to handle validation and
Expand Down
8 changes: 6 additions & 2 deletions src/main/scala/introcourse/level07/LogParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ object LogParser {
* - Warning
* - Error with (severity: Int)
*/
sealed trait LogLevel
enum LogLevel {
case TODO // enums must have as least one case, replace this when you are ready
}

/**
* Now create an ADT for `LogMessage`, where `LogMessage` can be one of two possibilities:
Expand All @@ -44,7 +46,9 @@ object LogParser {
*/
type Timestamp = Int

sealed trait LogMessage
enum LogMessage {
case TODO // enums must have as least one case, replace this when you are ready
}

/**
* Once you have defined your data types, remove `import Types._` from
Expand Down
3 changes: 2 additions & 1 deletion src/test/scala/introcourse/level02/TypesExercisesTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package introcourse.level02

import introcourse.level02.TypesExercises._
import introcourse.level02.TypesExercises.*
import introcourse.level02.TypesExercises.TrafficLight.*
import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.funspec.AnyFunSpec

Expand Down
3 changes: 2 additions & 1 deletion src/test/scala/introcourse/level04/NullExercisesTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package introcourse.level04

import introcourse.level04.NullExercises._
import introcourse.level04.NullExercises.*
import introcourse.level04.TrafficLight.*
import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.funspec.AnyFunSpec

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package introcourse.level04

import introcourse.level04.OptionExercises1._
import introcourse.level04.OptionExercises1.*
import introcourse.level04.TrafficLight.*
import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.funspec.AnyFunSpec

Expand Down
3 changes: 2 additions & 1 deletion src/test/scala/introcourse/level05/EitherExercisesTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package introcourse.level05

import introcourse.level05.EitherExercises._
import introcourse.level05.EitherExercises.*
import introcourse.level05.EitherExercises.AppError.*
import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.funspec.AnyFunSpec

Expand Down

0 comments on commit 2d4ebdf

Please sign in to comment.