Creating an enum

Enum does not exist in Dart as a built-in type. Enums provide additional type checking and thus, help enhance code maintainability. So what alternative do we have? Look at the code in project enum, where we want to differentiate the degree of an issue reported to us (we distinguish between the following levels: TRIVIAL, REGU LAR, IMPO RTANT, and CRITICAL).

How to do it...

The first way to achieve the creating an enum functionality is shown in enu m1.dart:

class IssueDegree {
  final _value;
  const IssueDegree(this._value);
  toString() => 'Enum.$_value';

static const TRIVIAL = const IssueDegree('TRIVIAL'),
static const REGULAR = const IssueDegree('REGULAR'),
static const IMPORTANT = const IssueDegree('IMPORTANT'),
static const CRITICAL = const IssueDegree('CRITICAL'),
}

void main() {
  var issueLevel = IssueDegree.IMPORTANT;
  // Warning and NoSuchMethodError for IssueLevel2:
  // There is no such getter ALARM in IssueDegree
  // var issueLevel2 = IssueDegree.ALARM;

    switch (issueLevel) {
      case IssueDegree.TRIVIAL:
        print("Ok, I'll sort it out during lunch");
        break;
      case IssueDegree.REGULAR:
        print("We'll assign it to Ellen, our programmer");
        break;
      case IssueDegree.IMPORTANT:
        print("Let's discuss it in a meeting tomorrow morning");
        break;
      case IssueDegree.CRITICAL:
        print('Warn the Boss!'),
        break;
    }
}

This snippet prints Let's discuss it in a meeting tomorrow morning.

An alternative way, shown in enu m2.dart, is to define the enum behavior in an abstract class and then to implement that, as shown in the following code:

import 'enum_abstract_class.dart';

class IssueDegree<String> extends Enum<String> {

   const IssueDegree(String val) : super (val);

static const IssueDegree TRIVIAL = const IssueDegree('TRIV);
static const IssueDegree REGULAR = const IssueDegree('REG'),
static const IssueDegree IMPORTANT = const IssueDegree('IMP'),
static const IssueDegree CRITICAL = const IssueDegree('CRIT'),
}

main() {
  assert(IssueDegree.REGULAR is IssueDegree);
  // switch code
}

The switch code of the first example also works for this implementation. To simplify the code, the const values can also be defined outside the class, as in enum3.dart. Then, it is no longer needed to precede them with the enum class name, as shown in the following code:

import 'enum_abstract_class.dart';

const IssueDegree TRIVIAL = const IssueDegree('TRIV'),
const IssueDegree REGULAR = const IssueDegree('REG'),
const IssueDegree IMPORTANT = const IssueDegree('IMP'),
const IssueDegree CRITICAL = const IssueDegree('CRIT'),

class IssueDegree<String> extends Enum<String> {
   const IssueDegree(String val) : super (val);
}

main() {
  assert(REGULAR is IssueDegree);

  var issueLevel = IMPORTANT;
     switch (issueLevel) {
       case TRIVIAL:
         print("Ok, I'll sort it out during lunch");
      break;
    // rest of the code
}

How it works...

The first option uses an enum class with a const constructor to set a private _value; the class contains the different values as constants. The constants can only be defined inside the class, and you get autocompletion (in Dart Editor or other editors with the Dart plugin) for them for free! In this way, you can use this enum-like class in a switch, and both dartanalyzer and the runtime point out the error to you if a non-existent value is used. The enum_class.dart file provides the template code for this case; make sure you create the constant values, as shown in the following code:

class Enum {
  final _value;
  const Enum(this._value);
  toString() => 'Enum.$_value';

  static const VAL1 = const Enum('VAL1'),
  static const VAL2 = const Enum('VAL2'),
  static const VAL3 = const Enum('VAL3'),
  static const VAL4 = const Enum('VAL4'),
  static const VAL5 = const Enum('VAL5'),
}

The second way uses an abstract class Enum (defined in enum_abstract_class.dart) that takes a generic parameter <T>, as shown in the following code:

abstract class Enum<T> {
  final T _value;
  const Enum(this._value);
  T get value => _value;
} 

Making the values top-level constants simplifies the code.

There's more...

The Ecma TC52 Dart Standards Committee has investigated a proposal for enums that will be discussed in September 2014 (refer to http://www.infoq.com/news/2014/07/ecma-dart-google), so providing built-in support for enums probably will be implemented in a future Dart version.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset