Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Un-sealed abstract class in hierarchy gets ignored on Scala 3.x #377

Open
BardurArantsson opened this issue Aug 18, 2023 · 1 comment
Open

Comments

@BardurArantsson
Copy link

BardurArantsson commented Aug 18, 2023

Hi,

This is perhaps not a bug, technically, but I found the behavior suprising.

It seems that 'intermediate' abstract classes in a hierarchy must themselves be explicitly marked as sealed for findValues to pick them up.

Here's a little reproducer:

import enumeratum.*

sealed abstract class MyEnum(name: String) extends EnumEntry

object MyEnum extends Enum[MyEnum] {

  abstract class Intermediate(name: String)
    extends MyEnum(name = name)

  case object Foo extends MyEnum("Foo")
  case object Bar extends Intermediate("Bar")

  val values: IndexedSeq[MyEnum] =
    findValues

  @main
  def main(): Unit =
    Console.println(s"values: $values")

}

Will print

values: Vector(Foo)

instead of the expected

values: Vector(Bar, Foo)

Marking the Intermediate abstract class with sealed makes it show up.

Now, technically, the un-sealed abstract class could be extended by code elsewhere (and so all potential subclasses cannot be known), but I found this behavior of simply omitting the Bar very surprising... kind of dangerous because the omission is silent.

Could the macros be changed to include Bar (per the example), or maybe to emit a compiler warning/error about the missing 'sealed'?

@lloydmeta
Copy link
Owner

I think a warning could make sense. Feel free to submit a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants