Enum families and overlapping enums
See original GitHub issueThis works:
sealed trait Foo extends EnumEntry
object Foo extends Enum[Foo] {
val values = findValues
case object Foo1 extends Foo
case object Foo2 extends Foo
sealed trait Bar extends Foo
object Bar extends Enum[Bar] {
val values = findValues
case object Bar1 extends Bar
case object Bar2 extends Bar
}
sealed trait Qux extends Foo
object Qux extends Enum[Qux] {
val values = findValues
case object Qux1 extends Qux
case object Qux2 extends Qux
}
}
What I want:
sealed trait Foo extends EnumEntry
object Foo extends Enum[Foo] {
val values = findValues
case object Foo1 extends Foo
case object Foo2 extends Foo
case object Bar1 extends Bar
case object Bar2 extends Bar
case object Qux1 extends Qux
case object Qux2 extends Qux
case object BarQux1 extends Bar with Qux
case object BarQux2 extends Bar with Qux
sealed trait Bar extends Foo
object Bar extends Enum[Bar] {
val values = findValues
}
sealed trait Qux extends Foo
object Qux extends Enum[Qux] {
val values = findValues
}
}
Now that compiles, but at runtime Bar.values
and Qux.values
are empty!
Work around with Shapeless:
trait EnumValues[A] {
def find: IndexedSeq[A]
}
object EnumValues {
def apply[A : EnumValues]: EnumValues[A] = implicitly
def find[A : EnumValues]: IndexedSeq[A] = apply[A].find
@nowarn("cat=unused")
implicit def instance[A, C <: Coproduct, V <: HList](implicit
generic: Generic.Aux[A, C],
witnesses: LiftAll.Aux[Witness.Aux, C, V],
toArraySeq: ToTraversable.Aux[V, ArraySeq, Witness.Lt[A]]
): EnumValues[A] = new EnumValues[A] {
val find = toArraySeq(witnesses.instances).map(_.value)
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Enums, overlapping values, C# - Stack Overflow
Let's take a simple example enum PrivilegeLevel { None, Reporter, Reviewer, Admin, DefaultForNewUser = None, DefaultForProjectOwner ...
Read more >Enum Class (System)
Define enumeration constants in powers of two, that is, 1, 2, 4, 8, and so on. This means the individual flags in combined...
Read more >Structs and Enums - C# in Simple Terms
A series of overlapping metal trusses on the side of a building. ... The other kind of special object we'll discuss is an...
Read more >5 more things you should know about enums in C# - ...
Use only powers of two when defining flags, so that you won't overlap values when combining more enums; this will give you also...
Read more >Peeking inside a Rust enum
Now, if we look at Rust enums... it's a different story. ... It's just like struct , except everything overlaps, and it has...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In your original post, you stated that your issue was that
Your
Bar.values
is declared asThere are no
Bar
s inside the scope of the immediately envelopingEnum[Bar]
.Last I checked Shapeless conjures its
Generic
and friends eventually usingknownDirectSubclasses
anyways, then sorts them (https://github.com/milessabin/shapeless/issues/892).In any case, you can get what you want by doing something like this:
Scastie: https://scastie.scala-lang.org/Adc4z1d3TXCh3UOhz4CM1A
That’s a really nice way to make it work and no need for shapeless. Thank you 👍 Closing this now. But WDYT about a compiler warning when
findValues
is empty?