Using case objects as enumerations

158 views
Skip to first unread message

Bruce Eckel

unread,
Oct 19, 2012, 2:44:36 PM10/19/12
to scala...@googlegroups.com
Is there some way to automatically generate the equivalent of Colors?

sealed trait Color
case object Red extends Color
case object Green extends Color
case object Blue extends Color
val Colors = Vector(Red, Green, Blue)

I suspect this could be done using scala 2.10 reflection but wondered if there was any simpler approach.

Naftoli Gugenheim

unread,
Oct 19, 2012, 3:18:56 PM10/19/12
to Bruce Eckel, scala...@googlegroups.com
I assume you know about Enumeration?

Bruce Eckel

unread,
Oct 19, 2012, 3:22:44 PM10/19/12
to Naftoli Gugenheim, scala...@googlegroups.com
Yes, thanks. This is the alternative approach, since, as I understand it, Enumerations don't get handled as well with match expressions. In particular, the compiler can tell you if you've missed a case using case objects.

-- Bruce Eckel

Bardur Arantsson

unread,
Oct 19, 2012, 5:12:18 PM10/19/12
to scala...@googlegroups.com
On 10/19/2012 09:22 PM, Bruce Eckel wrote:
> Yes, thanks. This is the alternative approach, since, as I understand it,
> Enumerations don't get handled as well with match expressions. In
> particular, the compiler can tell you if you've missed a case using case
> objects.

*sealed* case classes/objects only, I believe.

(Open/Closed strikes again.)

Som Snytt

unread,
Oct 19, 2012, 7:44:07 PM10/19/12
to Bruce Eckel, scala...@googlegroups.com
I happened to have this example, using as you say just reflection.  For some reason, colors are appealing in this context.  I suppose it awaits a macro solution, where you ask for the categories, something like Value[Spectral].

object Colors extends Enumeration {
  sealed trait Spectral
  sealed trait GirlsFavorite
  case object Red extends Val with Spectral
  case object Blue extends Val with Spectral
  case object Pink extends Val with GirlsFavorite
  case object Purple extends Val with GirlsFavorite
  val Magenta = Value

Nicholas Sterling

unread,
Oct 20, 2012, 6:03:39 PM10/20/12
to scala...@googlegroups.com
If you are willing to extend an Enum class (or I guess you could make a trait out of it), you could do something like this:

Naftoli Gugenheim

unread,
Oct 21, 2012, 5:26:46 PM10/21/12
to Bruce Eckel, scala...@googlegroups.com
On Fri, Oct 19, 2012 at 2:44 PM, Bruce Eckel <bruce...@gmail.com> wrote:

Perhaps reflection could get you a Set. But how would it order them?

Simon Ochsenreither

unread,
Oct 21, 2012, 6:12:37 PM10/21/12
to scala...@googlegroups.com, Naftoli Gugenheim
If you want to use enumerations, don't bother with scala.Enumeration, use Java's enums.
If you want something ADT-like, use the sealed-trait+case-object approach, but that way you don't get a list of all values (as you already figured out). Getting those is possible both at compile-time and at runtime in 2.10.

Naftoli Gugenheim

unread,
Oct 21, 2012, 6:19:43 PM10/21/12
to Simon Ochsenreither, scala...@googlegroups.com
On Sun, Oct 21, 2012 at 6:12 PM, Simon Ochsenreither <simon.och...@gmail.com> wrote:
If you want to use enumerations, don't bother with scala.Enumeration, use Java's enums.
If you want something ADT-like, use the sealed-trait+case-object approach, but that way you don't get a list of all values (as you already figured out). Getting those is possible both at compile-time and at runtime in 2.10.

Can you illustrate how? And am I correct that there's no way to know their order?

Bruce Eckel

unread,
Oct 21, 2012, 7:22:26 PM10/21/12
to Naftoli Gugenheim, scala...@googlegroups.com
Since macros allow you to do things at compile time, and sealing a trait means that the compiler knows about all the Color subtypes (so it can, for example, know whether a match expression is exhaustive), I'm wondering if a macro might be able to do it.
Reply all
Reply to author
Forward
0 new messages