java - How to add a method to Enumeration in Scala? -
in java could:
public enum enum { 1 { public string method() { return "1"; } }, 2 { public string method() { return "2"; } }, 3 { public string method() { return "3"; } }; public abstract string method(); } how do in scala?
edit / useful links:
here example of adding attributes scala enums extending enumeration.val class.
object planet extends enumeration { protected case class val(val mass: double, val radius: double) extends super.val { def surfacegravity: double = planet.g * mass / (radius * radius) def surfaceweight(othermass: double): double = othermass * surfacegravity } implicit def valuetoplanetval(x: value) = x.asinstanceof[val] val g: double = 6.67300e-11 val mercury = val(3.303e+23, 2.4397e6) val venus = val(4.869e+24, 6.0518e6) val earth = val(5.976e+24, 6.37814e6) val mars = val(6.421e+23, 3.3972e6) val jupiter = val(1.9e+27, 7.1492e7) val saturn = val(5.688e+26, 6.0268e7) val uranus = val(8.686e+25, 2.5559e7) val neptune = val(1.024e+26, 2.4746e7) } scala> planet.values.filter(_.radius > 7.0e6) res16: planet.valueset = planet.valueset(jupiter, saturn, uranus, neptune)
Comments
Post a Comment