Functions and Records
Typedefs for Function Shapes
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.
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');
}
mode ← add
10void main() {11 var mode = 'add';12 IntOp op = mode == 'add' ? add : multiply;values this stepaddmodeop ← <function add>
11var mode = 'add';12IntOp op = mode == 'add' ? add : multiply;13var result = apply(3, 4, op);values this step<function add>opaddmodecall ← 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>opcalls ← 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>opreturn value ← 7
7int add(int left, int right) => left + right;8int multiply(int left, int right) => left * right;values this step7return value3left4rightreturn value ← 7
3int apply(int left, int right, IntOp op) {4 return op(left, right);5}values this step7return valueresult ← 7
12IntOp op = mode == 'add' ? add : multiply;13var result = apply(3, 4, op);14print('$mode=$result');values this step7resultprint('$mode=$result');
13 var result = apply(3, 4, op);14 print('$mode=$result');15}outputadd=7values this stepaddmode7result
mode ← multiply
10void main() {11 var mode = 'multiply';12 IntOp op = mode == 'add' ? add : multiply;values this stepmultiplymodeop ← <function multiply>
11var mode = 'multiply';12IntOp op = mode == 'add' ? add : multiply;13var result = apply(3, 4, op);values this step<function multiply>opmultiplymodecall ← 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>opcalls ← 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>opreturn value ← 12
7int add(int left, int right) => left + right;8int multiply(int left, int right) => left * right;values this step12return value3left4rightreturn value ← 12
3int apply(int left, int right, IntOp op) {4 return op(left, right);5}values this step12return valueresult ← 12
12IntOp op = mode == 'add' ? add : multiply;13var result = apply(3, 4, op);14print('$mode=$result');values this step12resultprint('$mode=$result');
13 var result = apply(3, 4, op);14 print('$mode=$result');15}outputmultiply=12values 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`.