2

I have an original Java enumeration class, which I can get the corresponding integer value using:

Integer blackInteger = ColorEnum.BLACK.getValue()

public enum ColorEnum {

    BLACK(0), BLUE(1), RED(2);

    private int value;

    private ColorEnum(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

I then tried to write the enum class in Scala:

object ColorEnum extends Enumeration {
  val BLACK, BLUE, RED = Value
}

I am wondering how do I get the integer value of ColorEnum.BLACK in Scala?

Thanks a lot!

Edamame
  • 23,718
  • 73
  • 186
  • 320

1 Answers1

3

Use id like so:

ColorEnum.BLACK.id

Enumeration.Value in the Scaladoc.

Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
bjfletcher
  • 11,168
  • 4
  • 52
  • 67
  • There's a comment somewhere not to use ordinal in java for logic. It's safer to add a field, since if you change the enum, you change the ordinal. In scala, you can set the id explicitly, `val x = Value(100)`. But because of the bit set impl, it's safer to have a field for big values. By coincidence, I was about to help with https://issues.scala-lang.org/browse/SI-4803 – som-snytt Jun 06 '15 at 16:26