|
| 1 | +// Check usage and precedence of block arguments in expressions: |
| 2 | +fn main() { |
| 3 | + let v = [-1f, 0f, 1f, 2f, 3f]; |
| 4 | + |
| 5 | + // Statement form does not require parentheses: |
| 6 | + vec::iter(v) { |i| |
| 7 | + log(info, i); |
| 8 | + } |
| 9 | + |
| 10 | + // Usable at all: |
| 11 | + let any_negative = vec::any(v) { |e| float::negative(e) }; |
| 12 | + assert any_negative; |
| 13 | + |
| 14 | + // Higher precedence than assignments: |
| 15 | + any_negative = vec::any(v) { |e| float::negative(e) }; |
| 16 | + assert any_negative; |
| 17 | + |
| 18 | + // Higher precedence than unary operations: |
| 19 | + let abs_v = vec::map(v) { |e| float::abs(e) }; |
| 20 | + assert vec::all(abs_v) { |e| float::nonnegative(e) }; |
| 21 | + assert !vec::any(abs_v) { |e| float::negative(e) }; |
| 22 | + |
| 23 | + // Usable in funny statement-like forms: |
| 24 | + if !vec::any(v) { |e| float::positive(e) } { |
| 25 | + assert false; |
| 26 | + } |
| 27 | + alt vec::all(v) { |e| float::negative(e) } { |
| 28 | + true { fail "incorrect answer."; } |
| 29 | + false { } |
| 30 | + } |
| 31 | + alt 3 { |
| 32 | + _ when vec::any(v) { |e| float::negative(e) } { |
| 33 | + } |
| 34 | + _ { |
| 35 | + fail "wrong answer."; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + // Lower precedence than binary operations: |
| 41 | + let w = vec::foldl(0f, v, { |x, y| x + y }) + 10f; |
| 42 | + let y = vec::foldl(0f, v) { |x, y| x + y } + 10f; |
| 43 | + let z = 10f + vec::foldl(0f, v) { |x, y| x + y }; |
| 44 | + assert w == y; |
| 45 | + assert y == z; |
| 46 | + |
| 47 | + // They are not allowed as the tail of a block without parentheses: |
| 48 | + let w = |
| 49 | + if true { vec::any(abs_v, { |e| float::nonnegative(e) }) } |
| 50 | + else { false }; |
| 51 | + assert w; |
| 52 | +} |
0 commit comments