authorgravatar for dev@doubly.soDevin Bayer <dev@doubly.so> 2021-05-01 01:05:45+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-05-01 01:05:45+02:00
log5fcc922ff2e0cfc52122e0a00270463e4d58ff00
treed6cc8d852178876375a981635de487b90de151e0
parent33cb6608387d603f59b161c61db7255ef6e55704
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

add doc in `Anonymous Struct Literal` section for special @"0" syntax. (#8630)


1 files changed, 31 insertions(+), 0 deletions(-)

doc/langref.html.in+31
...@@ -2861,6 +2861,37 @@ fn dump(args: anytype) void {...@@ -2861,6 +2861,37 @@ fn dump(args: anytype) void {
2861 expect(args.b);2861 expect(args.b);
2862 expect(args.s[0] == 'h');2862 expect(args.s[0] == 'h');
2863 expect(args.s[1] == 'i');2863 expect(args.s[1] == 'i');
2864}
2865 {#code_end#}
2866 <p>
2867 Anonymous structs can be created without specifying field names, and are referred to as "tuples".
2868 </p>
2869 <p>
2870 The fields are implicitly named using numbers starting from 0. Because their names are integers,
2871 the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as identifiers.
2872 </p>
2873 <p>
2874 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#}.
2875 </p>
2876 {#code_begin|test|tuple#}
2877const std = @import("std");
2878const expect = std.testing.expect;
2879
2880test "tuple" {
2881 const values = .{
2882 @as(u32, 1234),
2883 @as(f64, 12.34),
2884 true,
2885 "hi",
2886 } ++ .{false} ** 2;
2887 expect(values[0] == 1234);
2888 expect(values[4] == false);
2889 inline for (values) |v, i| {
2890 if (i != 2) continue;
2891 expect(v);
2892 }
2893 expect(values.len == 6);
2894 expect(values.@"3"[0] == 'h');
2864}2895}
2865 {#code_end#}2896 {#code_end#}
2866 {#header_close#}2897 {#header_close#}