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
Replay: real traced execution (multi-file project)
(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');
}
  1. call ← minMax([7, 2, 9, 4])

    7void main() {8  var (lo, hi) = minMax([7, 2, 9, 4]);9  print('$lo $hi');
    values this stepminMax([7, 2, 9, 4])call
  2. lo ← 2

    1(int, int) minMax(List<int> xs) {2  var lo = xs.reduce((a, b) => a < b ? a : b);3  var hi = xs.reduce((a, b) => a > b ? a : b);
    values this step2lo[7, 2, 9, 4]xs
  3. hi ← 9

    2var lo = xs.reduce((a, b) => a < b ? a : b);3var hi = xs.reduce((a, b) => a > b ? a : b);4return (lo, hi);
    values this step9hi[7, 2, 9, 4]xs
  4. return value ← (2, 9)

    3  var hi = xs.reduce((a, b) => a > b ? a : b);4  return (lo, hi);5}
    values this step(2, 9)return value2lo9hi
  5. lo ← 2, hi ← 9

    7void main() {8  var (lo, hi) = minMax([7, 2, 9, 4]);9  print('$lo $hi');
    values this step2lo9hi
  6. print('$lo $hi');

    8  var (lo, hi) = minMax([7, 2, 9, 4]);9  print('$lo $hi');10}
    output2 9
    values this step2lo9hi
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.