Discussion:
[dart-misc] Introducing enum_class.dart
David Morgan ☯
2015-08-26 11:51:15 UTC
Permalink
Hi Dartisans,

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

https://github.com/google/enum_class.dart

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

Example class that you write:

https://github.com/google/enum_class.dart/blob/master/example/lib/test_enum.dart

Example generated code:

https://github.com/google/enum_class.dart/blob/master/example/lib/test_enum.g.dart

Example project structure, note the "tools/build.dart":
https://github.com/google/enum_class.dart/tree/master/example

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
--
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+***@dartlang.org.
Daniel Joyce
2015-08-27 21:35:31 UTC
Permalink
Richer enums like Java would be nice. Its rare, but I've found them handy.
Post by David Morgan ☯
Hi Dartisans,
For those who like a bit of class in their enums, or a bit of enum in
https://github.com/google/enum_class.dart
You write a class, declare the constants, and it generates the boilerplate
needed for enum-like behaviour.
https://github.com/google/enum_class.dart/blob/master/example/lib/test_enum.dart
https://github.com/google/enum_class.dart/blob/master/example/lib/test_enum.g.dart
https://github.com/google/enum_class.dart/tree/master/example
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
--
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
--
Daniel Joyce

The meek shall inherit the Earth, for the brave will be among the stars.
--
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+***@dartlang.org.
David Morgan ☯
2015-08-28 07:45:55 UTC
Permalink
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
Post by Daniel Joyce
Richer enums like Java would be nice. Its rare, but I've found them handy.
Post by David Morgan ☯
Hi Dartisans,
For those who like a bit of class in their enums, or a bit of enum in
https://github.com/google/enum_class.dart
You write a class, declare the constants, and it generates the
boilerplate needed for enum-like behaviour.
https://github.com/google/enum_class.dart/blob/master/example/lib/test_enum.dart
https://github.com/google/enum_class.dart/blob/master/example/lib/test_enum.g.dart
https://github.com/google/enum_class.dart/tree/master/example
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
--
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
--
Daniel Joyce
The meek shall inherit the Earth, for the brave will be among the stars.
--
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+***@dartlang.org.
Lasse Damgaard
2015-08-28 08:34:05 UTC
Permalink
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
Post by Daniel Joyce
Richer enums like Java would be nice. Its rare, but I've found them handy.
Post by David Morgan ☯
Hi Dartisans,
For those who like a bit of class in their enums, or a bit of enum in
https://github.com/google/enum_class.dart
You write a class, declare the constants, and it generates the
boilerplate needed for enum-like behaviour.
https://github.com/google/enum_class.dart/blob/master/example/lib/test_enum.dart
https://github.com/google/enum_class.dart/blob/master/example/lib/test_enum.g.dart
https://github.com/google/enum_class.dart/tree/master/example
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
--
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
--
Daniel Joyce
The meek shall inherit the Earth, for the brave will be among the stars.
--
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+***@dartlang.org.
David Morgan ☯
2015-08-28 09:14:03 UTC
Permalink
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.
Post by Lasse Damgaard
I actually miss full blown enums a lot in frontend code.
- 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
Post by Daniel Joyce
Richer enums like Java would be nice. Its rare, but I've found them handy.
Post by David Morgan ☯
Hi Dartisans,
For those who like a bit of class in their enums, or a bit of enum in
https://github.com/google/enum_class.dart
You write a class, declare the constants, and it generates the
boilerplate needed for enum-like behaviour.
https://github.com/google/enum_class.dart/blob/master/example/lib/test_enum.dart
https://github.com/google/enum_class.dart/blob/master/example/lib/test_enum.g.dart
https://github.com/google/enum_class.dart/tree/master/example
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.
we have a similar tool and over a hundred of these semi-generated enums on
my (Google internal) project.
Cheers
Morgan
--
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
--
Daniel Joyce
The meek shall inherit the Earth, for the brave will be among the stars.
--
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+***@dartlang.org.
Loading...