diff options
-rw-r--r-- | src/library/scala/Enumeration.scala | 11 | ||||
-rw-r--r-- | test/files/run/enums.check | 1 | ||||
-rw-r--r-- | test/files/run/enums.scala | 20 |
3 files changed, 29 insertions, 3 deletions
diff --git a/src/library/scala/Enumeration.scala b/src/library/scala/Enumeration.scala index 8d38f71f79..d5153bd50d 100644 --- a/src/library/scala/Enumeration.scala +++ b/src/library/scala/Enumeration.scala @@ -121,9 +121,14 @@ abstract class Enumeration(initial: Int, names: String*) { * initialize each Enumeration with Value(String). Otherwise, the * names are determined automatically through reflection. * - * @param s an enumeration name - * @return <tt>Some(Value)</tt> if an enumeration's name matches <var>s</var>, - * else <tt>None</tt> + * Note the change here wrt 2.7 is intentional. You should know whether + * a name is in an Enumeration beforehand. If not, just use find on + * values. + * + * @param s an Enumeration name + * @return the Value of this Enumeration if its name matches <var>s</var> + * @throws java.util.NoSuchElementException if no Value with a matching + * name is in this Enumeration */ final def withName(s: String): Value = values.find(_.toString == s).get diff --git a/test/files/run/enums.check b/test/files/run/enums.check index b76705b9dd..f53aba8794 100644 --- a/test/files/run/enums.check +++ b/test/files/run/enums.check @@ -1,4 +1,5 @@ test Test1 was successful test Test2 was successful test Test3 was successful +test Test4 was successful diff --git a/test/files/run/enums.scala b/test/files/run/enums.scala index 99986787b7..fcca8d3438 100644 --- a/test/files/run/enums.scala +++ b/test/files/run/enums.scala @@ -46,6 +46,25 @@ object Test3 { } } +object Test4 { + + object Direction extends Enumeration("North", "South", "East", "West") { + val North, South, East, West = Value; + } + + def run: Int = { + val dir = Direction.withName("North") + assert(dir.toString == "North") + try { + Direction.withName("Nord") + assert(false) + } catch { + case e: Exception => /* do nothing */ + } + 0 + } +} + //############################################################################ // Test code @@ -73,6 +92,7 @@ object Test { check_success("Test1", Test1.run, 5); check_success("Test2", Test2.run, 5); check_success("Test3", Test3.run, 1); + check_success("Test4", Test4.run, 0); Console.println; } } |