Scala Programming Language
  1. Scala Programming Language
  2. SI-3950

enumeration generates runtime exception in 2.8.x (worked in 2.7.x)

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Enumeration
    • Labels:
      None
    • Environment:

      enumeration

      Description

      The following code worked fine with Scala 2.7.x but now crashes at runtime with Scala 2.8.x :

      object test {
        object State extends Enumeration {
          val UNKNOWN = Value(-3, "UNKNOWN")
          val WIN     = Value(-2, "WIN")
          val EMPTY   = Value( 0, "EMPTY")
          val PLAYER1 = Value( 1, "PLAYER1")
          val PLAYER2 = Value( 2, "PLAYER2")
      // 2.7.x
      //  def fromInt(id: Int) = elements find (_.id == id) match {
      // 2.8.x
          def fromInt(id: Int) = values find (_.id == id) match {
            case Some(v) => v
            case None => UNKNOWN
          }
        }
        def main(args: Array[String]) {
          println(State.fromInt(-2))
          // 2.7.x ==> WIN
          // 2.8.x ==> java.lang.IllegalArgumentException:
          //           requirement failed: bitset element must be >= 0
        }
      }
      

      The original Java 1.5 code (cf. android-sdk/TicTacToeMain/) is:

      class test {
          public enum State {
              UNKNOWN(-3),
              WIN(-2),
              EMPTY(0),
              PLAYER1(1),
              PLAYER2(2);
      
              private int mValue;
      
              private State(int value) {
                  mValue = value;
              }
              public int getValue() {
                  return mValue;
              }
              public static State fromInt(int i) {
                  for (State s : values()) {
                      if (s.getValue() == i) {
                          return s;
                      }
                  }
                  return EMPTY;
              }
          }
          public static void main(String[] args) {
              System.out.println(State.fromInt(-2)); // ==> WIN
          }
      }
      

        Issue Links

          Activity

          Hide
          Martin Odersky added a comment -

          Did the code work in 2.8.0?

          Show
          Martin Odersky added a comment - Did the code work in 2.8.0?
          Hide
          Paul Phillips added a comment -

          Replying to [comment:1 odersky]:
          > Did the code work in 2.8.0?

          It does not work in 2.8.0.

          Show
          Paul Phillips added a comment - Replying to [comment:1 odersky] : > Did the code work in 2.8.0? It does not work in 2.8.0.
          Hide
          Hubert Plociniczak added a comment -

          Actually, I will take it since the previous enumeration bugs are also assigned to me

          Show
          Hubert Plociniczak added a comment - Actually, I will take it since the previous enumeration bugs are also assigned to me
          Hide
          Hubert Plociniczak added a comment -

          (In r23893) Closes SI-3687, SI-3719, SI-3950, SI-3616. Plus removed some deprecated stuff for 2.9. Review by extempore

          Show
          Hubert Plociniczak added a comment - (In r23893) Closes SI-3687 , SI-3719 , SI-3950 , SI-3616 . Plus removed some deprecated stuff for 2.9. Review by extempore

            People

            • Assignee:
              Hubert Plociniczak
              Reporter:
              Stephane Micheloud
              TracCC:
              Paul Phillips
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development