Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
scabug opened this issue Oct 22, 2010 · 5 comments
Closed

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

scabug opened this issue Oct 22, 2010 · 5 comments
Assignees
Labels

Comments

@scabug
Copy link

scabug commented Oct 22, 2010

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
    }
}
@scabug
Copy link
Author

scabug commented Oct 22, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3950?orig=1
Reporter: @michelou
See #5211

@scabug
Copy link
Author

scabug commented Oct 25, 2010

@odersky said:
Did the code work in 2.8.0?

@scabug
Copy link
Author

scabug commented Oct 25, 2010

@paulp said:
Replying to [comment:1 odersky]:

Did the code work in 2.8.0?

It does not work in 2.8.0.

@scabug
Copy link
Author

scabug commented Dec 13, 2010

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

@scabug
Copy link
Author

scabug commented Jan 4, 2011

@hubertp said:
(In r23893) Closes #3687, #3719, #3950, #3616. Plus removed some deprecated stuff for 2.9. Review by extempore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants