Overriding Scala Enumeration Value

后端 未结 4 878
孤街浪徒
孤街浪徒 2020-12-05 02:35

As far as I can tell, Scala has definitions for the Enumeration Value class for Value(Int), Value(String), and Value(Int, String).

Does anyone know of an example for

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 02:56

    Here is another simpler approach:

    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    object Colors extends Enumeration {
      sealed case class Color private[Colors](hexCode: String, name: String) extends Val(name)
    
      val Black = Color("#000000", "black")
      val White = Color("#FFFFFF", "white")
    }
    
    // Exiting paste mode, now interpreting.
    
    defined object Colors
    
    scala> Colors.Black.hexCode
    res0: String = #000000
    
    scala> Colors.Black.name
    res1: String = black
    
    scala> Colors.values
    res2: Colors.ValueSet = Colors.ValueSet(black, white)
    
    scala> 
    

提交回复
热议问题