Functions and Records
Records and Destructuring
A record (a, b) groups values without declaring a class. var (x, y) = record destructures them into bindings.
Program
Play the program to return min and max as a record and destructure them.
records_destructure.dart
(int, int) minMax(List<int> xs) {
var lo = xs.reduce((a, b) => a < b ? a : b);
var hi = xs.reduce((a, b) => a > b ? a : b);
return (lo, hi);
}
void main() {
var (lo, hi) = minMax([7, 2, 9, 4]);
print('$lo $hi');
}
record type
`(int, int)` is a record with two positional fields.
destructuring
`var (lo, hi) = ...` pulls each field into a name.
multiple return
Records let a function return more than one value without a class.