| 1 | fn f0(cb fn () int) int { |
| 2 | return cb() * 10 |
| 3 | } |
| 4 | |
| 5 | fn f1(cb fn (a int) int) int { |
| 6 | return cb(10) |
| 7 | } |
| 8 | |
| 9 | fn f2(cb fn (a int, b int) int) int { |
| 10 | return cb(10, 10) |
| 11 | } |
| 12 | |
| 13 | fn f3(cb fn (a int, b int, c int) int) int { |
| 14 | return cb(10, 10, 10) |
| 15 | } |
| 16 | |
| 17 | enum MyEnum { |
| 18 | no |
| 19 | xyz = 4 |
| 20 | other = 10 |
| 21 | } |
| 22 | |
| 23 | fn f3_different(cb fn (a int, b string, c MyEnum) string) string { |
| 24 | return cb(10, 'abc', .xyz) |
| 25 | } |
| 26 | |
| 27 | fn test_lambda_expr() { |
| 28 | assert f0(|| 4) == 40 |
| 29 | assert f1(|x| x + 4) == 14 |
| 30 | assert f2(|xx, yy| xx + yy + 4) == 24 |
| 31 | assert f3(|xxx, yyy, zzz| xxx + yyy + zzz + 4) == 34 |
| 32 | assert f3_different(|xxx, yyy, zzz| yyy + ',${xxx}, ${yyy}, ${zzz}') == 'abc,10, abc, xyz' |
| 33 | } |
| 34 | |
| 35 | fn doit(x int, y int, cb fn (a int, b int) string) string { |
| 36 | dump(cb) |
| 37 | dump(x) |
| 38 | dump(y) |
| 39 | return cb(x, y) |
| 40 | } |
| 41 | |
| 42 | fn test_fn_with_callback_called_with_lambda_expression() { |
| 43 | assert doit(10, 20, fn (aaa int, bbb int) string { |
| 44 | return 'a: ${aaa}, b: ${bbb}' |
| 45 | }) == 'a: 10, b: 20' |
| 46 | assert doit(100, 200, |a, b| 'a: ${a}, b: ${b}') == 'a: 100, b: 200' |
| 47 | } |
| 48 | |
| 49 | // for test params has blank_ident |
| 50 | fn f4(g fn (int) string) { |
| 51 | assert g(0) == 'hello' |
| 52 | } |
| 53 | |
| 54 | fn test_params_has_blank_ident() { |
| 55 | f4(|_| 'hello') |
| 56 | } |
| 57 | |
| 58 | fn test_lambda_expr_can_omit_unused_callback_params() { |
| 59 | assert f1(|| 4) == 4 |
| 60 | assert f2(|x| x + 4) == 14 |
| 61 | } |
| 62 | |