Overriding Scala Enumeration Value

后端 未结 4 872
孤街浪徒
孤街浪徒 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条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 03:15

    I would prefer doing it by extending the Enumeration.Val class.

    For your requirement, I would post a sample below:

    object FileType extends Enumeration {
      val csv = Val(1,"csv", ",")
      val tsv = Val(2,"tsv", "\t")
      protected case class Val(num: Int, fileType: String, delimiter: String) extends super.Val
      implicit def valueToFileType(x: Value): Val = x.asInstanceOf[Val]
    }
    

    Accessing values is as below:

    scala> FileType.csv
    res0: FileType.Val = csv
    
    scala> FileType.csv.delimiter
    res29: String = ,
    

提交回复
热议问题