Records in Dart
Bundle values without a class; the shape is the type.
// A record bundles values without a class; the shape is the type.
(double, double) hideLocation() => (52.74, 0.39);
({String species, int count}) bestOfDay() =>
(species: 'marsh harrier', count: 4);
void main() {
// Positional records destructure by position.
final (lat, lon) = hideLocation();
print('Hide at $lat N, $lon E');
// Named fields read by name, destructured or dotted.
final best = bestOfDay();
print('${best.count} ${best.species}');
final (:species, :count) = bestOfDay();
print('$species x$count');
}
How it works
(double, double)is a positional record type.({String species, int count})names its fields.- Destructure positionally, by
.field, or with(:species, :count).
Keywords and builtins used here
Stringdoublefinalintvoid
The run, in numbers
- Lines
- 17
- Characters to type
- 520
- Tokens
- 120
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 111 seconds.
Step 1 of 3 in Records & patterns, step 19 of 27 in Language basics.