Sunday, October 18, 2015

Enumeration in Scala

object Example {
  object Direction extends Enumeration {
    val North, East, South, West = Value
  }
}

object Test {

  import Example._
  {
    for(direction <- Direction.values) {
      println(direction)
    }
  }

}

You can customize string representation of enumerations:

object Example {
  object Direction extends Enumeration {
    val North = Value("N")
    val East = Value("E")
    val South = Value("S")
    val West = Value("W")
  }
}

object Test {

  import Example._
  {
    for(direction <- Direction.values) {
      println(direction)
    }
  }

}

0 comments: