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

Naming of enumeration fields relies on unspecified behaviour of the JVM

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: Scala 2.9.1
    • Fix Version/s: None
    • Component/s: Enumeration
    • Labels:

      Description

      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.

        Issue Links

          Activity

          There are no comments yet on this issue.

            People

            • Assignee:
              Unassigned
              Reporter:
              Stefan Zeiger
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:

                Development