Introducing enum_class.dart

135 views
Skip to first unread message

David Morgan ☯

unread,
Aug 26, 2015, 7:51:15 AM8/26/15
to Dart Misc
Hi Dartisans,

For those who like a bit of class in their enums, or a bit of enum in their classes, a tool:


You write a class, declare the constants, and it generates the boilerplate needed for enum-like behaviour.

Example class that you write:

Example generated code:

Example project structure, note the "tools/build.dart":

The source_gen library is still bleeding edge, you'll need to be on SDK 1.12 to use enum_class.

Feedback welcome! I'm really interested in whether people find this useful and how we can make it more usable.

Although the implementation is bleeding edge, the idea is well tested: we have a similar tool and over a hundred of these semi-generated enums on my (Google internal) project.

Cheers

Morgan

Daniel Joyce

unread,
Aug 27, 2015, 5:35:44 PM8/27/15
to Dart Misc
Richer enums like Java would be nice. Its rare, but I've found them handy.


--
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.
--
Daniel Joyce

The meek shall inherit the Earth, for the brave will be among the stars.

David Morgan ☯

unread,
Aug 28, 2015, 3:45:56 AM8/28/15
to Dart Misc
Yes, I hope they end up in some future version of Dart.

In the meantime, enum_class is actually exactly designed to match Java functionality. On my project we keep Java and Dart equivalents for the same enum, and use them in RPCs. So we want the Dart enums to be able to carry code / implement interfaces, like the Java ones.

A quick look at the big picture / master plan:

 + built_collection.dart: released and stable, immutable collections with the builder pattern
 + enum_class.dart: just released, Java-like enums
 + built_value.dart: coming soon, codegen to help build nice immutable value types
 + built_json.dart: coming a bit later :) ... JSON serialization for all of the above: collections, enums, values
 + Built JSON for Java: Java equivalent serialization so you can use all of the above for RPCs; on the Java side you'll use @AutoValue

Cheers

Morgan

Lasse Damgaard

unread,
Aug 28, 2015, 4:34:05 AM8/28/15
to Dart Misc
I actually miss full blown enums a lot in frontend code. 
Typical scenarios are:

 - Defining a sort order.
 - Converting values to a pretty print display string and vice versa.
 - Converting to/from JSON from my backend.

Doing all of this stuff in utility methods outside of the enum is ugly and means I'm always torn between whether to use enums or not.

 Lasse

David Morgan ☯

unread,
Aug 28, 2015, 5:14:03 AM8/28/15
to Dart Misc
Exactly.

My approach is to forbid the use of native Dart enums.

By using native enums you create a trap for developers. If someone wants to add functionality to a native enum they might be lazy and just add utility method, when the correct thing is to convert to a class. So, start with a class and get it right from the start.

BTW, there's one more feature coming to enum_class, for Angular developers: we can generate a mixin class for you to mix in to your component class. It will expose the enum constants to your template so you can use them directly in the template.
Reply all
Reply to author
Forward
0 new messages