Skip to content

Enum: incosistence of simple case encoding with case object #23583

@dos65

Description

@dos65

3.7.0

I had a code that uses enum and class tags which compiles and looks fine but works incorrect due to specifics of encoding for simple case.

It took me some time to realize that simple enum case isn't equal to sealed trait + case object encoding and it doesn't actually creates a type.

Example that puzzled me:

enum Token:
   case Dot
   case Space
   case Word(v: String)

def read[T <: Token](t: Token)(using ct: ClassTag[T]): T =
   t match
      case t: T =>   t
      case _ => throw new Exception(s"$t is not an instance of $ct")

read[Token.Dot.type](Token.Space) // <- no error - while I was expecting to get it
read[Token.Word](Token.Space)      // <- throws error - ok

The ambiguity in this example:

  • ClassTag[Token.Word] - is Token.Word everything is fine
  • ClassTag[Token.Dot.type] - is Token, there is not Token.Dot type

Expectation

For each enum case there is a proper type and they are diffrerent from each other.

The questions is if it's possible to switch this encoding now or it's too late?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions