Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I build an enum with Dart? [duplicate]

Tags:

dart

The Dart language does not have enums (yet??). What is the proper or idiomatic way to construct an enum, at least until a language feature arrives?

like image 919
Seth Ladd Avatar asked Apr 06 '13 18:04

Seth Ladd


People also ask

Can enum have duplicate values?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

Can enum be subclassed?

We've learned that we can't create a subclass of an existing enum. However, an interface is extensible. Therefore, we can emulate extensible enums by implementing an interface.

Can enum implement interface in Dart?

Enums are still restricted, you cannot implement the interface other than by creating an enum .


2 Answers

Dart now has support for enums.

The rest of this answer is for Dart <= 1.8. If using > 1.8, use Dart's formal support for enums (explained in another answer).

It's true, the Dart language does not (yet?) have enums. There is an open issue for it.

In the meantime, here is an idiomatic Dart snippet to create your own enum.

class Enum {   final _value;   const Enum._internal(this._value);   toString() => 'Enum.$_value';    static const FOO = const Enum._internal('FOO');   static const BAR = const Enum._internal('BAR');   static const BAZ = const Enum._internal('BAZ'); } 

Using const constructors means you can use this enum in a switch. Here's an example:

class Fruits {   final _value;   const Fruits._internal(this._value);   toString() => 'Enum.$_value';    static const APPLE = const Fruits._internal('APPLE');   static const PEAR = const Fruits._internal('PEAR');   static const BANANA = const Fruits._internal('BANANA'); }  void main() {   var yummy = Fruits.BANANA;    switch (yummy) {     case Fruits.APPLE:       print('an apple a day');       break;     case Fruits.PEAR:       print('genus Pyrus in the family Rosaceae');       break;     case Fruits.BANANA:       print('open from the bottom, it is easier');       break;   } } 
like image 132
Seth Ladd Avatar answered Sep 27 '22 18:09

Seth Ladd


With r41815 Dart got native Enum support see http://dartbug.com/21416 and can be used like

enum Status {   none,   running,   stopped,   paused }  void main() {   print(Status.values);   Status.values.forEach((v) => print('value: $v, index: ${v.index}'));   print('running: ${Status.running}, ${Status.running.index}');   print('running index: ${Status.values[1]}'); } 

[Status.none, Status.running, Status.stopped, Status.paused]
value: Status.none, index: 0
value: Status.running, index: 1
value: Status.stopped, index: 2
value: Status.paused, index: 3
running: Status.running, 1
running index: Status.running

A limitation is that it is not possibly to set custom values for an enum item, they are automatically numbered.

More details at in this draft https://www.dartlang.org/docs/spec/EnumsTC52draft.pdf

like image 26
Günter Zöchbauer Avatar answered Sep 27 '22 18:09

Günter Zöchbauer