authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-11 19:57:57-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-11 19:57:57-05:00
log0c315e7f7613b085a203e9c94d222e846b5b9e46
tree81ced0f54f4c02eb7fd3bdd5d3d1248014f356a6
parentb9482fc32d13886626692484e5a778355fa7934c
signaturelock-open Commit is signed but in an unrecognized format.

update docs for anonymous struct/list literals


1 files changed, 112 insertions(+), 1 deletions(-)

doc/langref.html.in+112-1
......@@ -1734,6 +1734,43 @@ test "array initialization with function calls" {
17341734 {#code_end#}
17351735 {#see_also|for|Slices#}
17361736
1737 {#header_open|Anonymous List Literals#}
1738 <p>Similar to {#link|Enum Literals#} and {#link|Anonymous Struct Literals#}
1739 the type can be omitted from array literals:</p>
1740 {#code_begin|test|anon_list#}
1741const std = @import("std");
1742const assert = std.debug.assert;
1743
1744test "anonymous list literal syntax" {
1745 var array: [4]u8 = .{11, 22, 33, 44};
1746 assert(array[0] == 11);
1747 assert(array[1] == 22);
1748 assert(array[2] == 33);
1749 assert(array[3] == 44);
1750}
1751 {#code_end#}
1752 <p>
1753 If there is no type in the result location then an anonymous list literal actually
1754 turns into a {#link|struct#} with numbered field names:
1755 </p>
1756 {#code_begin|test|infer_list_literal#}
1757const std = @import("std");
1758const assert = std.debug.assert;
1759
1760test "fully anonymous list literal" {
1761 dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"});
1762}
1763
1764fn dump(args: var) void {
1765 assert(args.@"0" == 1234);
1766 assert(args.@"1" == 12.34);
1767 assert(args.@"2");
1768 assert(args.@"3"[0] == 'h');
1769 assert(args.@"3"[1] == 'i');
1770}
1771 {#code_end#}
1772 {#header_close#}
1773
17371774 {#header_open|Multidimensional Arrays#}
17381775 <p>
17391776 Mutlidimensional arrays can be created by nesting arrays:
......@@ -2526,7 +2563,8 @@ test "overaligned pointer to packed struct" {
25262563 Don't worry, there will be a good solution for this use case in zig.
25272564 </p>
25282565 {#header_close#}
2529 {#header_open|struct Naming#}
2566
2567 {#header_open|Struct Naming#}
25302568 <p>Since all structs are anonymous, Zig infers the type name based on a few rules.</p>
25312569 <ul>
25322570 <li>If the struct is in the initialization expression of a variable, it gets named after
......@@ -2552,6 +2590,53 @@ fn List(comptime T: type) type {
25522590}
25532591 {#code_end#}
25542592 {#header_close#}
2593
2594 {#header_open|Anonymous Struct Literals#}
2595 <p>
2596 Zig allows omitting the struct type of a literal. When the result is {#link|coerced|Type Coercion#},
2597 the struct literal will directly instantiate the result location, with no copy:
2598 </p>
2599 {#code_begin|test|struct_result#}
2600const std = @import("std");
2601const assert = std.debug.assert;
2602
2603const Point = struct {x: i32, y: i32};
2604
2605test "anonymous struct literal" {
2606 var pt: Point = .{
2607 .x = 13,
2608 .y = 67,
2609 };
2610 assert(pt.x == 13);
2611 assert(pt.y == 67);
2612}
2613 {#code_end#}
2614 <p>
2615 The struct type can be inferred. Here the result location does not include a type, and
2616 so Zig infers the type:
2617 </p>
2618 {#code_begin|test|struct_anon#}
2619const std = @import("std");
2620const assert = std.debug.assert;
2621
2622test "fully anonymous struct" {
2623 dump(.{
2624 .int = @as(u32, 1234),
2625 .float = @as(f64, 12.34),
2626 .b = true,
2627 .s = "hi",
2628 });
2629}
2630
2631fn dump(args: var) void {
2632 assert(args.int == 1234);
2633 assert(args.float == 12.34);
2634 assert(args.b);
2635 assert(args.s[0] == 'h');
2636 assert(args.s[1] == 'i');
2637}
2638 {#code_end#}
2639 {#header_close#}
25552640 {#see_also|comptime|@fieldParentPtr#}
25562641 {#header_close#}
25572642 {#header_open|enum#}
......@@ -2906,6 +2991,32 @@ test "@tagName" {
29062991 <p>A {#syntax#}packed union{#endsyntax#} has well-defined in-memory layout and is eligible
29072992 to be in a {#link|packed struct#}.
29082993 {#header_close#}
2994
2995 {#header_open|Anonymous Union Literals#}
2996 <p>{#link|Anonymous Struct Literals#} syntax can be used to initialize unions without specifying
2997 the type:</p>
2998 {#code_begin|test|anon_union#}
2999const std = @import("std");
3000const assert = std.debug.assert;
3001
3002const Number = union {
3003 int: i32,
3004 float: f64,
3005};
3006
3007test "anonymous union literal syntax" {
3008 var i: Number = .{.int = 42};
3009 var f = makeNumber();
3010 assert(i.int == 42);
3011 assert(f.float == 12.34);
3012}
3013
3014fn makeNumber() Number {
3015 return .{.float = 12.34};
3016}
3017 {#code_end#}
3018 {#header_close#}
3019
29093020 {#header_close#}
29103021
29113022 {#header_open|blocks#}