Enhanced enums in Dart
Dart enums are classes: fields, constructors, members.
// Dart enums are classes: fields, constructors, and members.
enum Tide {
low(0.4),
mid(1.1),
high(2.3);
final double meters;
const Tide(this.meters);
bool get wadersFeeding => this != Tide.high;
}
void main() {
for (final tide in Tide.values) {
print('${tide.name}: ${tide.meters} m, '
'waders ${tide.wadersFeeding ? 'feeding' : 'roosting'}');
}
}
How it works
- Each value calls the const constructor:
low(0.4). final double metersrides along on every value.wadersFeedingis a getter computed per value;.nameprints itself.
Keywords and builtins used here
boolconstdoublefinalforgetinthisvoid
The run, in numbers
- Lines
- 18
- Characters to type
- 351
- Tokens
- 95
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 88 seconds.
Step 3 of 3 in Classes, step 15 of 27 in Language basics.