Enums in Scala 3
New enum syntax.
The New enum Keyword
Scala 3 adds a first-class enum construct. It replaces the verbose sealed-trait-plus-case-objects pattern from Scala 2 with concise syntax.
- Each case becomes a value of the enum type.
- The compiler generates useful helpers automatically.
enum Color:
case Red, Green, Blue
object Main:
def main(args: Array[String]): Unit =
println(Color.Red)Listing All Values
Every simple enum gets a companion with values (an array of all cases) and valueOf (lookup by name).
enum Color:
case Red, Green, Blue
object Main:
def main(args: Array[String]): Unit =
Color.values.foreach(println)
println(Color.valueOf("Green"))All lessons in this course
- Significant Indentation
- Enums in Scala 3
- Opaque Types
- Union and Intersection Types