| 1 | type GuardedFn = fn (arg Bar) bool |
| 2 | |
| 3 | type GuardedResultFn = fn (arg Bar) !bool |
| 4 | |
| 5 | struct GuardedFoo { |
| 6 | f ?GuardedFn |
| 7 | } |
| 8 | |
| 9 | struct GuardedResultFoo { |
| 10 | f ?GuardedResultFn |
| 11 | } |
| 12 | |
| 13 | @[params] |
| 14 | struct Bar {} |
| 15 | |
| 16 | fn (foo GuardedFoo) call(arg Bar) bool { |
| 17 | if foo.f != none { |
| 18 | // vfmt off |
| 19 | return foo.f or { panic(err) }(arg) |
| 20 | // vfmt on |
| 21 | } |
| 22 | return false |
| 23 | } |
| 24 | |
| 25 | fn (foo GuardedResultFoo) call(arg Bar) bool { |
| 26 | if foo.f != none { |
| 27 | // vfmt off |
| 28 | return foo.f or { panic(err) }(arg) or { return false } |
| 29 | // vfmt on |
| 30 | } |
| 31 | return false |
| 32 | } |
| 33 | |
| 34 | fn test_optional_fn_selector_call_in_if_guard() { |
| 35 | foo := GuardedFoo{ |
| 36 | f: fn (arg Bar) bool { |
| 37 | _ = arg |
| 38 | return true |
| 39 | } |
| 40 | } |
| 41 | assert foo.call(Bar{}) |
| 42 | } |
| 43 | |
| 44 | fn test_optional_fn_selector_call_with_nested_or_blocks() { |
| 45 | ok := GuardedResultFoo{ |
| 46 | f: fn (arg Bar) !bool { |
| 47 | _ = arg |
| 48 | return true |
| 49 | } |
| 50 | } |
| 51 | failing := GuardedResultFoo{ |
| 52 | f: fn (arg Bar) !bool { |
| 53 | _ = arg |
| 54 | return error('boom') |
| 55 | } |
| 56 | } |
| 57 | assert ok.call(Bar{}) |
| 58 | assert !failing.call(Bar{}) |
| 59 | } |
| 60 | |