From f7027732a1d1e92f4d7525f2a984a24fdb7a0053 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 5 Apr 2017 17:09:15 +0200 Subject: Implementation of proposal changes - rename utility methods - generate utility methods also for object cases --- tests/run/enum-Color.scala | 2 +- tests/run/enum-Option.scala | 8 ++++---- tests/run/planets.scala | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 tests/run/planets.scala (limited to 'tests') diff --git a/tests/run/enum-Color.scala b/tests/run/enum-Color.scala index 1a077bf8e..836e02c62 100644 --- a/tests/run/enum-Color.scala +++ b/tests/run/enum-Color.scala @@ -4,7 +4,7 @@ enum Color { object Test { def main(args: Array[String]) = - for (color <- Color.values) { + for (color <- Color.enumValues) { println(s"$color: ${color.enumTag}") assert(Color.valueOf(color.enumTag) eq color) } diff --git a/tests/run/enum-Option.scala b/tests/run/enum-Option.scala index b9efadf0d..76f5641b3 100644 --- a/tests/run/enum-Option.scala +++ b/tests/run/enum-Option.scala @@ -1,12 +1,12 @@ -enum class Option[+T] extends Serializable { +enum class Option[+T >: Null] extends Serializable { def isDefined: Boolean } object Option { - def apply[T](x: T): Option[T] = if (x == null) None else Some(x) + def apply[T >: Null](x: T): Option[T] = if (x == null) None else Some(x) case Some(x: T) { def isDefined = true } - case None extends Option[Nothing] { + case None { def isDefined = false } } @@ -14,6 +14,6 @@ object Option { object Test { def main(args: Array[String]) = { assert(Some(None).isDefined) - Option(22) match { case Option.Some(x) => assert(x == 22) } + Option("22") match { case Option.Some(x) => assert(x == "22") } } } diff --git a/tests/run/planets.scala b/tests/run/planets.scala new file mode 100644 index 000000000..2fff01edc --- /dev/null +++ b/tests/run/planets.scala @@ -0,0 +1,26 @@ +enum class Planet(mass: Double, radius: Double) { + private final val G = 6.67300E-11 + def surfaceGravity = G * mass / (radius * radius) + def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity +} +object Planet { + case MERCURY extends Planet(3.303e+23, 2.4397e6) + case VENUS extends Planet(4.869e+24, 6.0518e6) + case EARTH extends Planet(5.976e+24, 6.37814e6) + case MARS extends Planet(6.421e+23, 3.3972e6) + case JUPITER extends Planet(1.9e+27, 7.1492e7) + case SATURN extends Planet(5.688e+26, 6.0268e7) + case URANUS extends Planet(8.686e+25, 2.5559e7) + case NEPTUNE extends Planet(1.024e+26, 2.4746e7) +} +object Test { + def main(args: Array[String]) = { + import Planet._ + assert(enumValueNamed("SATURN") == SATURN) + assert(enumValue(2) == EARTH) + val earthWeight = args(0).toDouble + val mass = earthWeight/EARTH.surfaceGravity + for (p <- enumValues) + println(s"Your weight on $p is ${p.surfaceWeight(mass)}") + } +} -- cgit v1.2.3