How to add a method to Enumeration in Scala?
enumeration, enums, java, scala
Solution
object Unit extends Enumeration {
abstract class UnitValue(var name: String) extends Val(name) {
def m: Unit
}
val G = new UnitValue("g") {
def m {
println("M from G")
}
}
val KG = new UnitValue("kg") {
def m {
println("M from KG")
}
}
}
Problem
In Java you could: ``` public enum Enum { ONE { public String method() { return "1"; } }, TWO { public String method() { return "2"; } }, THREE { public String method() { return "3"; } }; public abstract String method(); } ``` How do you do this in Scala? EDIT / Useful links: - https://github.com/rbricks/itemized - http://pedrorijo.com/blog/scala-enums/