Skip to content

Enumeration and type erasure #3815

@scabug

Description

@scabug

From the API Enumeration:

"All values in an enumeration share a common, unique type defined as the Value type member of the enumeration (Value selected on the stable identifier path of the enumeration instance)."

But there is a type erasure within Enumeration class.
That can give an unexpected type error. Example:

object X extends Enumeration { val x,y = Value }
object A extends Enumeration { val a,b = Value }

X.Value and A.Value are different types. So the functions

  def pp( z:X.Value) {}
  def pp( z:A.Value) {}

should be different too, but you get a compile error:

error: double definition:
method pp:(z: A.Value)Unit and
method pp:(z: X.Value)Unit at line 34
have same type after erasure: (z: Enumeration#Value)Unit
  def pp( z:A.Value) {}

BTW I found a curious workaround

  def pp( z:X.Value) { println("X") }
  def pp( z:A.Value)(implicit t:Int=0) { println("A") }

  pp(X.x)
  pp(A.a)

There is no compile error any more and the output shows
"X" and "A" as expected.

Frank

Activity

scabug

scabug commented on Aug 31, 2010

@scabug
Author

Imported From: https://issues.scala-lang.org/browse/SI-3815?orig=1
Reporter: Frank Teubler (dft)
See #5211

scabug

scabug commented on Sep 1, 2010

@scabug
Author

@paulp said:
Yikes. I say it's time to admit Enumeration is a failed experiment and go back to the drawing board.

BTW if you like that workaround here's [http://scala-programming-language.1934581.n4.nabble.com/disambiguation-of-double-definition-resulting-from-generic-type-erasure-tp2327664p2327853.html a more refined version].

scabug

scabug commented on Sep 1, 2010

@scabug
Author

@lrytz said:
yes, enumeration needs a redesign. see also #3719, #3687

scabug

scabug commented on Jan 4, 2011

@scabug
Author

@odersky said:
I think it's a won't fix. That's just what Enumeration and Java erasure do.

scabug

scabug commented on Nov 4, 2011

@scabug
Author

@paulp said:
Martin came around and agrees this is a bug. Reopening.

added this to the Backlog milestone on Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @odersky@scabug

        Issue actions

          Enumeration and type erasure · Issue #3815 · scala/bug