A typedef gives a reusable name to a function type. This keeps callback-heavy code readable: the variable, parameter, and helper all agree on the same function shape without repeating int Function(int, int) everywhere.

Program

Play the program to store one binary operation alias, switch it with a selector, and call it through a helper.

mode
typedef_function_aliases.dart
Replay: real traced execution (multi-file project)
typedef IntOp = int Function(int left, int right);

int apply(int left, int right, IntOp op) {
  return op(left, right);
}

int add(int left, int right) => left + right;
int multiply(int left, int right) => left * right;

void main() {
  var mode = 'add';
  IntOp op = mode == 'add' ? add : multiply;
  var result = apply(3, 4, op);
  print('$mode=$result');
}
typedef IntOp = int Function(int left, int right);

int apply(int left, int right, IntOp op) {
  return op(left, right);
}

int add(int left, int right) => left + right;
int multiply(int left, int right) => left * right;

void main() {
  var mode = 'multiply';
  IntOp op = mode == 'add' ? add : multiply;
  var result = apply(3, 4, op);
  print('$mode=$result');
}
  1. mode ← add

    10void main() {11  var mode = 'add';12  IntOp op = mode == 'add' ? add : multiply;
    values this stepaddmode
  2. op ← <function add>

    11var mode = 'add';12IntOp op = mode == 'add' ? add : multiply;13var result = apply(3, 4, op);
    values this step<function add>opaddmode
  3. call ← apply(3, 4, add)

    12IntOp op = mode == 'add' ? add : multiply;13var result = apply(3, 4, op);14print('$mode=$result');
    values this stepapply(3, 4, add)call<function add>op
  4. calls ← op(3, 4) -> add(3, 4)

    3int apply(int left, int right, IntOp op) {4  return op(left, right);5}
    values this stepop(3, 4) -> add(3, 4)calls3left4right<function add>op
  5. return value ← 7

    7int add(int left, int right) => left + right;8int multiply(int left, int right) => left * right;
    values this step7return value3left4right
  6. return value ← 7

    3int apply(int left, int right, IntOp op) {4  return op(left, right);5}
    values this step7return value
  7. result ← 7

    12IntOp op = mode == 'add' ? add : multiply;13var result = apply(3, 4, op);14print('$mode=$result');
    values this step7result
  8. print('$mode=$result');

    13  var result = apply(3, 4, op);14  print('$mode=$result');15}
    outputadd=7
    values this stepaddmode7result
  1. mode ← multiply

    10void main() {11  var mode = 'multiply';12  IntOp op = mode == 'add' ? add : multiply;
    values this stepmultiplymode
  2. op ← <function multiply>

    11var mode = 'multiply';12IntOp op = mode == 'add' ? add : multiply;13var result = apply(3, 4, op);
    values this step<function multiply>opmultiplymode
  3. call ← apply(3, 4, multiply)

    12IntOp op = mode == 'add' ? add : multiply;13var result = apply(3, 4, op);14print('$mode=$result');
    values this stepapply(3, 4, multiply)call<function multiply>op
  4. calls ← op(3, 4) -> multiply(3, 4)

    3int apply(int left, int right, IntOp op) {4  return op(left, right);5}
    values this stepop(3, 4) -> multiply(3, 4)calls3left4right<function multiply>op
  5. return value ← 12

    7int add(int left, int right) => left + right;8int multiply(int left, int right) => left * right;
    values this step12return value3left4right
  6. return value ← 12

    3int apply(int left, int right, IntOp op) {4  return op(left, right);5}
    values this step12return value
  7. result ← 12

    12IntOp op = mode == 'add' ? add : multiply;13var result = apply(3, 4, op);14print('$mode=$result');
    values this step12result
  8. print('$mode=$result');

    13  var result = apply(3, 4, op);14  print('$mode=$result');15}
    outputmultiply=12
    values this stepmultiplymode12result
typedef `typedef IntOp = int Function(int left, int right);` names a function shape once.
alias in declarations `IntOp op` is easier to read than repeating the full function type at every variable or parameter.
same runtime behavior A typedef is a static type alias; calling `op(3, 4)` still runs the actual function value stored in `op`.