| ... | @@ -1734,6 +1734,43 @@ test "array initialization with function calls" { | ... | @@ -1734,6 +1734,43 @@ test "array initialization with function calls" { |
| 1734 | {#code_end#} | 1734 | {#code_end#} |
| 1735 | {#see_also|for|Slices#} | 1735 | {#see_also|for|Slices#} |
| 1736 | | 1736 | |
| | 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#} |
| | 1741 | const std = @import("std"); |
| | 1742 | const assert = std.debug.assert; |
| | 1743 | |
| | 1744 | test "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#} |
| | 1757 | const std = @import("std"); |
| | 1758 | const assert = std.debug.assert; |
| | 1759 | |
| | 1760 | test "fully anonymous list literal" { |
| | 1761 | dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"}); |
| | 1762 | } |
| | 1763 | |
| | 1764 | fn 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 | |
| 1737 | {#header_open|Multidimensional Arrays#} | 1774 | {#header_open|Multidimensional Arrays#} |
| 1738 | <p> | 1775 | <p> |
| 1739 | Mutlidimensional arrays can be created by nesting arrays: | 1776 | Mutlidimensional arrays can be created by nesting arrays: |
| ... | @@ -2526,7 +2563,8 @@ test "overaligned pointer to packed struct" { | ... | @@ -2526,7 +2563,8 @@ test "overaligned pointer to packed struct" { |
| 2526 | Don't worry, there will be a good solution for this use case in zig. | 2563 | Don't worry, there will be a good solution for this use case in zig. |
| 2527 | </p> | 2564 | </p> |
| 2528 | {#header_close#} | 2565 | {#header_close#} |
| 2529 | {#header_open|struct Naming#} | 2566 | |
| | 2567 | {#header_open|Struct Naming#} |
| 2530 | <p>Since all structs are anonymous, Zig infers the type name based on a few rules.</p> | 2568 | <p>Since all structs are anonymous, Zig infers the type name based on a few rules.</p> |
| 2531 | <ul> | 2569 | <ul> |
| 2532 | <li>If the struct is in the initialization expression of a variable, it gets named after | 2570 | <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 { | ... | @@ -2552,6 +2590,53 @@ fn List(comptime T: type) type { |
| 2552 | } | 2590 | } |
| 2553 | {#code_end#} | 2591 | {#code_end#} |
| 2554 | {#header_close#} | 2592 | {#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#} |
| | 2600 | const std = @import("std"); |
| | 2601 | const assert = std.debug.assert; |
| | 2602 | |
| | 2603 | const Point = struct {x: i32, y: i32}; |
| | 2604 | |
| | 2605 | test "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#} |
| | 2619 | const std = @import("std"); |
| | 2620 | const assert = std.debug.assert; |
| | 2621 | |
| | 2622 | test "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 | |
| | 2631 | fn 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#} |
| 2555 | {#see_also|comptime|@fieldParentPtr#} | 2640 | {#see_also|comptime|@fieldParentPtr#} |
| 2556 | {#header_close#} | 2641 | {#header_close#} |
| 2557 | {#header_open|enum#} | 2642 | {#header_open|enum#} |
| ... | @@ -2906,6 +2991,32 @@ test "@tagName" { | ... | @@ -2906,6 +2991,32 @@ test "@tagName" { |
| 2906 | <p>A {#syntax#}packed union{#endsyntax#} has well-defined in-memory layout and is eligible | 2991 | <p>A {#syntax#}packed union{#endsyntax#} has well-defined in-memory layout and is eligible |
| 2907 | to be in a {#link|packed struct#}. | 2992 | to be in a {#link|packed struct#}. |
| 2908 | {#header_close#} | 2993 | {#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#} |
| | 2999 | const std = @import("std"); |
| | 3000 | const assert = std.debug.assert; |
| | 3001 | |
| | 3002 | const Number = union { |
| | 3003 | int: i32, |
| | 3004 | float: f64, |
| | 3005 | }; |
| | 3006 | |
| | 3007 | test "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 | |
| | 3014 | fn makeNumber() Number { |
| | 3015 | return .{.float = 12.34}; |
| | 3016 | } |
| | 3017 | {#code_end#} |
| | 3018 | {#header_close#} |
| | 3019 | |
| 2909 | {#header_close#} | 3020 | {#header_close#} |
| 2910 | | 3021 | |
| 2911 | {#header_open|blocks#} | 3022 | {#header_open|blocks#} |