authorgravatar for yujiri@disroot.orgEvin Yulo <yujiri@disroot.org> 2022-12-10 03:10:55+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-01-03 13:29:27+02:00
logfc07e1a2670970084bfaac726428f6f3392abf30
tree788f27e994bd2c9e8946f69453d075ca722c1c95
parenta44085dc2ab81877fc71fb50e846454c2694a14c

Document tuple syntax

Closes #13837

1 files changed, 24 insertions(+), 40 deletions(-)

doc/langref.html.in+24-40
......@@ -2433,43 +2433,6 @@ test "array initialization with function calls" {
24332433 {#code_end#}
24342434 {#see_also|for|Slices#}
24352435
2436 {#header_open|Anonymous List Literals#}
2437 <p>Similar to {#link|Enum Literals#} and {#link|Anonymous Struct Literals#}
2438 the type can be omitted from array literals:</p>
2439 {#code_begin|test|anon_list#}
2440const std = @import("std");
2441const expect = std.testing.expect;
2442
2443test "anonymous list literal syntax" {
2444 var array: [4]u8 = .{11, 22, 33, 44};
2445 try expect(array[0] == 11);
2446 try expect(array[1] == 22);
2447 try expect(array[2] == 33);
2448 try expect(array[3] == 44);
2449}
2450 {#code_end#}
2451 <p>
2452 If there is no type in the {#link|result location|Result Location Semantics#} then an
2453 anonymous list literal actually turns into a {#link|struct#} with numbered field names:
2454 </p>
2455 {#code_begin|test|infer_list_literal#}
2456const std = @import("std");
2457const expect = std.testing.expect;
2458
2459test "fully anonymous list literal" {
2460 try dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"});
2461}
2462
2463fn dump(args: anytype) !void {
2464 try expect(args.@"0" == 1234);
2465 try expect(args.@"1" == 12.34);
2466 try expect(args.@"2");
2467 try expect(args.@"3"[0] == 'h');
2468 try expect(args.@"3"[1] == 'i');
2469}
2470 {#code_end#}
2471 {#header_close#}
2472
24732436 {#header_open|Multidimensional Arrays#}
24742437 <p>
24752438 Multidimensional arrays can be created by nesting arrays:
......@@ -3578,15 +3541,21 @@ fn dump(args: anytype) !void {
35783541 try expect(args.s[1] == 'i');
35793542}
35803543 {#code_end#}
3544 {#header_close#}
3545
3546 {#header_open|Tuples#}
35813547 <p>
35823548 Anonymous structs can be created without specifying field names, and are referred to as "tuples".
35833549 </p>
35843550 <p>
35853551 The fields are implicitly named using numbers starting from 0. Because their names are integers,
3586 the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as {#link|identifiers|Identifiers#}.
3552 they cannot be accessed with {#syntax#}.{#endsyntax#} syntax without also wrapping them in
3553 {#syntax#}@""{#endsyntax#}. Names inside {#syntax#}@""{#endsyntax#} are always recognised as
3554 {#link|identifiers|Identifiers#}.
35873555 </p>
35883556 <p>
3589 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#}.
3557 Like arrays, tuples have a .len field, can be indexed (provided the index is comptime-known)
3558 and work with the ++ and ** operators. They can also be iterated over with {#link|inline for#}.
35903559 </p>
35913560 {#code_begin|test|tuple#}
35923561const std = @import("std");
......@@ -6488,7 +6457,22 @@ test "coercion between unions and enums" {
64886457 {#see_also|union|enum#}
64896458 {#header_close#}
64906459 {#header_open|Type Coercion: undefined#}
6491 <p>{#link|undefined#} can be cast to any type.</p>
6460 <p>{#link|undefined#} can be coerced to any type.</p>
6461 {#header_close#}
6462
6463 {#header_open|Type Coercion: tuples to arrays#}
6464 <p>{#link|Tuples#} can be coerced to arrays, if all of the fields have the same type.</p>
6465 {#code_begin|test|test_coerce_tuples_arrays#}
6466const std = @import("std");
6467const expect = std.testing.expect;
6468
6469const Tuple = struct{ u8, u8 };
6470test "coercion from homogenous tuple to array" {
6471 const tuple: Tuple = .{5, 6};
6472 const array: [2]u8 = tuple;
6473 _ = array;
6474}
6475 {#code_end#}
64926476 {#header_close#}
64936477 {#header_close#}
64946478