| author | |
| committer | |
| log | 2d3eece3f4c6ab67165aab2c6c0bc890ad2338ea |
| tree | 2aab9c01841fe9d700b65780fae621ca2e940280 |
| parent | 12ead9415ca1dfb15803c46d4598141daee314ba |
7 files changed, 80 insertions(+), 0 deletions(-)
doc/langref.html.in+2| ... | ... | @@ -1908,6 +1908,8 @@ or |
| 1908 | 1908 | = *= *%= *|= /= %= += +%= +|= -= -%= -|= <<= <<|= >>= &= ^= |={#endsyntax#}</pre> |
| 1909 | 1909 | {#header_close#} |
| 1910 | 1910 | {#header_close#} |
| 1911 | {#header_close#} | |
| 1912 | ||
| 1911 | 1913 | {#header_open|Arrays#} |
| 1912 | 1914 | {#code|test_arrays.zig#} |
| 1913 | 1915 |
doc/langref/ambiguous_precedence_mul_bitand.zig created+13| ... | ... | @@ -0,0 +1,13 @@ |
| 1 | test "ambiguous precedence" { | |
| 2 | const x: u8 = 1; | |
| 3 | const y: u8 = 2; | |
| 4 | const z: u8 = 3; | |
| 5 | ||
| 6 | // Its not obvious below if this is multiplication by a masked value | |
| 7 | // `(x&y) *% z` | |
| 8 | // or a masking of wrapped multiplication | |
| 9 | // `x & (y*%z)` | |
| 10 | _ = x & y *% z; | |
| 11 | } | |
| 12 | ||
| 13 | // obj=ambiguous operator precedence with operators … & … *% …; use parentheses to disambiguate | |
| \ No newline at end of file |
doc/langref/ambiguous_precedence_orelse_ne.zig created+21| ... | ... | @@ -0,0 +1,21 @@ |
| 1 | fn makeBool(x: u32) ?bool { | |
| 2 | if (x == 0) return false; | |
| 3 | if (x == 1) return true; | |
| 4 | return null; | |
| 5 | } | |
| 6 | ||
| 7 | test "ambiguous precedence" { | |
| 8 | const x: ?u32 = 0; | |
| 9 | const y: u8 = 0; | |
| 10 | const z: u8 = 0; | |
| 11 | ||
| 12 | // We might expect the line below to parse something like: | |
| 13 | // `makeBool(x) orelse (y != z)` | |
| 14 | // but we might elsewhere want something like | |
| 15 | // `(nullable(x) orelse y) != w` | |
| 16 | // to perform a fallback and then a check, either way its | |
| 17 | // not obvious the behavior so we get an error | |
| 18 | _ = makeBool(x) orelse y != z; | |
| 19 | } | |
| 20 | ||
| 21 | // obj=ambiguous operator precedence with operators … orelse … != …; use parentheses to disambiguate | |
| \ No newline at end of file |
doc/langref/ambiguous_precedence_shift_sub.zig created+10| ... | ... | @@ -0,0 +1,10 @@ |
| 1 | test "ambiguous precedence" { | |
| 2 | const x: u64 = 1; | |
| 3 | const y: u6 = 63; | |
| 4 | ||
| 5 | // We probably meant `(x << y) - 1` | |
| 6 | // But thats not how it would have compiled | |
| 7 | _ = x << y - 1; | |
| 8 | } | |
| 9 | ||
| 10 | // obj=ambiguous operator precedence with operators … << … - …; use parentheses to disambiguate | |
| \ No newline at end of file |
doc/langref/chained_sibling_operators.zig created+5| ... | ... | @@ -0,0 +1,5 @@ |
| 1 | fn foo(x: anytype, y: anytype, z: anytype) void { | |
| 2 | _ = x + y - z; | |
| 3 | } | |
| 4 | ||
| 5 | // syntax | |
| \ No newline at end of file |
doc/langref/nonchaining_sibling_operators.zig created+5| ... | ... | @@ -0,0 +1,5 @@ |
| 1 | fn foo(x: anytype, y: anytype, z: anytype) void { | |
| 2 | _ = x << y << z; | |
| 3 | } | |
| 4 | ||
| 5 | // obj=repetition of non-chainable operators … << … << …; use parentheses to disambiguate | |
| \ No newline at end of file |
doc/langref/operator_precedence.zig created+24| ... | ... | @@ -0,0 +1,24 @@ |
| 1 | const assert = @import("std").debug.assert; | |
| 2 | ||
| 3 | test "operator precedence" { | |
| 4 | const x = 1; | |
| 5 | const y = 2; | |
| 6 | const z = 3; | |
| 7 | ||
| 8 | // Because comparisons have a lower precedence than any arithmetic | |
| 9 | // We can safely avoid parenthesizing the expressions | |
| 10 | assert(x + y * z == x + (y*z)); | |
| 11 | assert(x * y + z == (x*y) + z); | |
| 12 | ||
| 13 | // We can also use ambiguous operators as long as they are seperated | |
| 14 | // by a well ordered one such as below | |
| 15 | assert(x*y + x%y == (x*y) + (x%y)); | |
| 16 | //That includes comparisons | |
| 17 | assert(x & y != x + y); | |
| 18 | ||
| 19 | // We can freely chain or's and and's, but not with each other | |
| 20 | assert(false or x == y or x + y == z); | |
| 21 | assert(true and x != y and y != z); | |
| 22 | } | |
| 23 | ||
| 24 | // test | |
| \ No newline at end of file |