How to get correct value type when extending Scala Enumeration.Val

a 夏天 提交于 2019-12-05 16:13:28

Try to cast enum value to Planet using asInstanceOf[Planet]

object PlanetEnum extends Enumeration {

  // universal gravitational constant (m3 kg-1 s-2)
  val G = 6.67300E-11

  val Mercury = Planet(3.303e+23, 2.4397e6)
  val Venus = Planet(4.869e+24, 6.0518e6)
  val Earth = Planet(5.976e+24, 6.37814e6)
  val Mars = Planet(6.421e+23, 3.3972e6)
  val Jupiter = Planet(1.9e+27, 7.1492e7)
  val Saturn = Planet(5.688e+26, 6.0268e7)
  val Uranus = Planet(8.686e+25, 2.5559e7)
  val Neptune = Planet(1.024e+26, 2.4746e7)

  case class Planet(mass: Double, radius: Double) extends Val {

    def surfaceGravity: Double = G * mass / (radius * radius)

    def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity

  }

}

object PlayEnumeration extends App {

  val earthWeight = 175
  val mass = earthWeight / PlanetEnum.Earth.surfaceGravity

  PlanetEnum.values.foreach {
    p => println(s"Your weight on $p is ${p.asInstanceOf[Planet].surfaceWeight(mass)}")

  }

  println

}
chaotic3quilibrium

Add the following line to object Planet:

implicit def convert(value: Value): Planet = value.asInstanceOf[Planet]

This leverages the extraordinary power of implicits within Scala.

If you are looking for something that might give you even more flexibility (especially exhaustive pattern matching), I just posted an answer showing my general solution for enumerations within Scala.

After doing extensive research on options, the solution is a much more complete overview of this domain including solving the "sealed trait + case object" pattern where I finally solved the JVM class/object initialization ordering problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!