There's an upcoming feature in Dart known as enhanced enums, and it allows for enum declarations with many of the features known from classes. For example:
enum ErrorCode {
None(0),
Unknown(1),
ConnectionLost(100),
OutlierReading(200);
final int value;
const ErrorCode(this.value);
}
The feature is not yet released (and note that several things are not yet working), but experiments with it can be performed with a suitably fresh version of the tools by passing --enable-experiment=enhanced-enums
.
The outcome is that ErrorCode
is an enum declaration with four values ErrorCode.None
, ErrorCode.Unknown
, and so on, and we have ErrorCode.None.value == 0
and ErrorCode.Unknown.value == 1
, and so on. The current bleeding edge handles this example in the common front end (so dart
and dart2js
will handle it), but it is not yet handled by the analyzer.