| ... | @@ -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#} |
| | 2877 | const std = @import("std"); |
| | 2878 | const expect = std.testing.expect; |
| | 2879 | |
| | 2880 | test "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#} |