Skip to content

Naming of enumeration fields relies on unspecified behaviour of the JVM #5462

Open
@scabug

Description

@scabug

On Oracle JDK:

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object State extends Enumeration {
     |   val IDLE, STARTED = Value
     |   var x = IDLE
     | }
defined module State

scala> State.x
res0: State.Value = IDLE

scala> State.IDLE
res1: State.Value = IDLE 

On OpenJDK, it prints "x" instead of "IDLE" because the name of the var is accidentally picked up instead of the intended field name. This is implementation-dependent: "The elements in the array returned are not sorted and are not in any particular order" ([http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getMethods()]).

There's probably not much we can do about this unless we can use Scala reflection instead of Java reflection to get the fields in the correct order.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions