aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevin Bayer <dev@doubly.so>2021-05-01 01:05:45 +0200
committerGitHub <noreply@github.com>2021-05-01 01:05:45 +0200
commit5fcc922ff2e0cfc52122e0a00270463e4d58ff00 (patch)
treed6cc8d852178876375a981635de487b90de151e0
parent33cb6608387d603f59b161c61db7255ef6e55704 (diff)
downloadzig-5fcc922ff2e0cfc52122e0a00270463e4d58ff00.tar.gz
zig-5fcc922ff2e0cfc52122e0a00270463e4d58ff00.zip
add doc in `Anonymous Struct Literal` section for special @"0" syntax. (#8630)
-rw-r--r--doc/langref.html.in31
1 files changed, 31 insertions, 0 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in
index 05bdfbd043..744d33f85c 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -2863,6 +2863,37 @@ fn dump(args: anytype) void {
expect(args.s[1] == 'i');
}
{#code_end#}
+ <p>
+ Anonymous structs can be created without specifying field names, and are referred to as "tuples".
+ </p>
+ <p>
+ The fields are implicitly named using numbers starting from 0. Because their names are integers,
+ the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as identifiers.
+ </p>
+ <p>
+ Like arrays, tuples have a .len field, can be indexed and work with the ++ and ** operators. They can also be iterated over with {#link|inline for#}.
+ </p>
+ {#code_begin|test|tuple#}
+const std = @import("std");
+const expect = std.testing.expect;
+
+test "tuple" {
+ const values = .{
+ @as(u32, 1234),
+ @as(f64, 12.34),
+ true,
+ "hi",
+ } ++ .{false} ** 2;
+ expect(values[0] == 1234);
+ expect(values[4] == false);
+ inline for (values) |v, i| {
+ if (i != 2) continue;
+ expect(v);
+ }
+ expect(values.len == 6);
+ expect(values.@"3"[0] == 'h');
+}
+ {#code_end#}
{#header_close#}
{#see_also|comptime|@fieldParentPtr#}
{#header_close#}