scala enum

Scala Enum

In Scala, there is no built-in keyword or construct for defining enums like in some other programming languages. However, you can achieve similar functionality by using sealed traits and case objects.

Here's an example of how you can define an "enum" in Scala using sealed traits and case objects:

sealed trait MyEnum
case object Value1 extends MyEnum
case object Value2 extends MyEnum
case object Value3 extends MyEnum

In this example, MyEnum is a sealed trait, which means that all its subtypes must be defined within the same file. The case object keyword is used to define the individual enum values (Value1, Value2, Value3).

You can then use these enum values in pattern matching or as regular objects:

def processEnumValue(value: MyEnum): Unit = value match {
  case Value1 => println("Processing Value1")
  case Value2 => println("Processing Value2")
  case Value3 => println("Processing Value3")
}

val myValue: MyEnum = Value2
processEnumValue(myValue)

This is a basic example of how you can achieve enum-like behavior in Scala. Keep in mind that sealed traits and case objects provide more flexibility and extensibility compared to traditional enums in other languages.