Thursday, February 11, 2016

Scala Enumerations

object EnumerationTypes extends App {

  object WeekDay extends Enumeration {
    type WeekDay = Value
    val Mon = Value("Mon")
    val Tue = Value("Tue")
    val Wed = Value("Wed")
    val Thu = Value("Thu")
    val Fri = Value("Fri")
    val Sat = Value("Sat")
    val Sun = Value("Sun")
    // Returns the value by name, if the name does not exist in the Enumeration, then return None
    def valueOf(name: String) = WeekDay.values.find(_.toString == name)
  }
  import WeekDay._

  def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun)
  val x = WeekDay.valueOf("Tue")

  WeekDay.values filter isWorkingDay foreach println
}

0 comments: