Packages

p

io.github.memo33

scalaenum

package scalaenum

Type Members

  1. abstract class Enum extends Serializable

    Defines a finite set of values specific to the enumeration.

    Defines a finite set of values specific to the enumeration. Typically these values enumerate all possible forms something can take and provide a lightweight alternative to case classes.

    Each call to a Value method adds a new unique value to the enumeration. To be accessible, these values are usually defined as val members of the enumeration.

    All values in an enumeration share a common, unique type defined as the abstract Value type member of the enumeration (Value selected on the stable identifier path of the enumeration instance). Besides, in contrast to Scala's built-in Enumeration, the Value type member can be extended in subclasses, such that it is possible to mix-in traits, for example. In this case, make sure to make the constructor of Value private. Example:

    // adding methods to Value
    class Day private extends Day.Val {
      def isWorkingDay: Boolean = this != Day.Saturday && this != Day.Sunday
    }
    object Day extends Enum {
      type Value = Day
      val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = new Day
    }
    
    // usage:
    Day.values filter (_.isWorkingDay) foreach println
    // output:
    // Monday
    // Tuesday
    // Wednesday
    // Thursday
    // Friday
    Annotations
    @SerialVersionUID()
    Example:
    1. // Example of adding attributes to an enumeration by extending the Enumeration.Val class
      object Planet extends Enumeration {
        protected case class PlanetVal(mass: Double, radius: Double) extends super.Val {
          def surfaceGravity: Double = Planet.G * mass / (radius * radius)
          def surfaceWeight(otherMass: Double): Double = otherMass * surfaceGravity
        }
        import scala.language.implicitConversions
        implicit def valueToPlanetVal(x: Value): PlanetVal = x.asInstanceOf[PlanetVal]
      
        val G: Double = 6.67300E-11
        val Mercury = PlanetVal(3.303e+23, 2.4397e6)
        val Venus   = PlanetVal(4.869e+24, 6.0518e6)
        val Earth   = PlanetVal(5.976e+24, 6.37814e6)
        val Mars    = PlanetVal(6.421e+23, 3.3972e6)
        val Jupiter = PlanetVal(1.9e+27, 7.1492e7)
        val Saturn  = PlanetVal(5.688e+26, 6.0268e7)
        val Uranus  = PlanetVal(8.686e+25, 2.5559e7)
        val Neptune = PlanetVal(1.024e+26, 2.4746e7)
      }
      
      println(Planet.values.filter(_.radius > 7.0e6))
      // output:
      // Planet.ValueSet(Jupiter, Saturn, Uranus, Neptune)

Ungrouped