Can't annotate enum values

104 views
Skip to first unread message

Don Olmstead

unread,
May 11, 2015, 7:29:08 PM5/11/15
to mi...@dartlang.org
I've been putting together a serialization library and one thing I wanted to support was serialization of enums. I attempted the following

enum States {
  @SerializationValue('AL')
  Alabama,
  @SerializationValue('AK')
  Alaska,
  ...
}

This causes an analyzer error "An enum must declare at least one constant name". Additionally I see https://code.google.com/p/dart/issues/detail?id=22178#c4 which says adding metadata to enum values is currently disallowed in the spec. So I'm curious why this is the case.

As a workaround I could have a const List on the enum itself but that feels more error prone if someone adds an enumeration or shuffles around the ordering.

Bob Nystrom

unread,
May 11, 2015, 7:51:06 PM5/11/15
to General Dart Discussion, Gilad Bracha

On Mon, May 11, 2015 at 4:29 PM, Don Olmstead <don.j.o...@gmail.com> wrote:
This causes an analyzer error "An enum must declare at least one constant name". Additionally I see https://code.google.com/p/dart/issues/detail?id=22178#c4 which says adding metadata to enum values is currently disallowed in the spec. So I'm curious why this is the case.

+Gilad

- bob

Jim Trainor

unread,
May 11, 2015, 7:56:43 PM5/11/15
to mi...@dartlang.org
I continue to do all my enums as const classes. I find the enum support in Dart too simple for all but the most basic cases.   Just a class with a final private data member (the enum's underlying value, typically a string), a single const private constructor, and static const public instances that use the private constructor that declare the enum constants.  More boiler plater than plain enum but not too hard, and the way that such a pseudo enum is referenced ends up being identical to built in enums.  To that class I add various to/from/valueOf/values/etc utilities methods as needed. 

class XYZ {
  static const A = const XYZ._final("eh");
  static const B = const XYZ._final("be");
  static const C = const XYZ._final("see");

  final String _value;

  const XYZ._final(this._value);

  String get value => value;
  /// etc... methods as needed
}


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Bob Nystrom

unread,
May 11, 2015, 8:14:04 PM5/11/15
to General Dart Discussion

On Mon, May 11, 2015 at 4:56 PM, Jim Trainor <jim.train...@gmail.com> wrote:
I continue to do all my enums as const classes. I find the enum support in Dart too simple for all but the most basic cases.

I do too. At the very least, almost every enum I define exposes its name publicly.

I really think we should improve the enum support in Dart. Otherwise, it's just not worth having at all.

- bob



Don Olmstead

unread,
May 11, 2015, 8:31:57 PM5/11/15
to mi...@dartlang.org
This is one issue I've hit the other being you can't do anything like a Flag<T extends enum> style class. Other than that I'm very happy with enums.

Cogman

unread,
May 12, 2015, 3:09:24 PM5/12/15
to mi...@dartlang.org
Yeah, I'm glad we have enums, but I'm somewhat disappointed at the conservative nature of them.  I really think that dart should go down the java route for enum capabilities.  It is just so convenient to have these named singletons filled with useful relevant data.

Don Olmstead

unread,
May 12, 2015, 3:41:44 PM5/12/15
to mi...@dartlang.org
We had this discussion on enums. It was a big bug. If you want Java enums you can still use classes.

I just want to know why there is a restriction on annotations for enum values and if it can be removed.

Don Olmstead

unread,
May 12, 2015, 5:42:33 PM5/12/15
to mi...@dartlang.org
Added https://code.google.com/p/dart/issues/detail?id=23441 if you want to attach Gilad to it Bob it'd be appreciated.

Thanks!

Anders Holmgren

unread,
May 13, 2015, 12:06:56 AM5/13/15
to mi...@dartlang.org
+1 for improving enums. In the meantime I hope someone writes a refactor in IntelliJ to turn an enum into a class. I often start with an enum and convert it when I need more

Günter Zöchbauer

unread,
May 13, 2015, 1:15:59 AM5/13/15
to mi...@dartlang.org
I use live templates for enum classes as a workaround, this is not too comfortable, but still better than writing everything manually.
Reply all
Reply to author
Forward
0 new messages