diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-09-03 11:32:39 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-09-03 11:32:39 -0400 |
| commit | 2a9329c9988430cfa11a8bb4b02da0dce8749028 (patch) | |
| tree | 3d28b4c95037be90a5dbe735728b2b07184dbd96 /doc | |
| parent | 95636c7e5ff6ad0eeb768a2a0a1d7533b5872e20 (diff) | |
| download | zig-2a9329c9988430cfa11a8bb4b02da0dce8749028.tar.gz zig-2a9329c9988430cfa11a8bb4b02da0dce8749028.zip | |
better anonymous struct naming
this makes anonymous structs inherit the name of the function they are in
only when they are the return expression.
also document the behavior and provide examples.
closes #1243
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/langref.html.in | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in index 31d923e84b..97263347c7 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -1918,6 +1918,32 @@ test "linked list" { assert(list2.first.?.data == 1234); } {#code_end#} + {#header_open|struct naming#} + <p>Since all structs are anonymous, Zig infers the type name based on a few rules.</p> + <ul> + <li>If the struct is in the initialization expression of a variable, it gets named after + that variable.</li> + <li>If the struct is in the <code>return</code> expression, it gets named after + the function it is returning from, with the parameter values serialized.</li> + <li>Otherwise, the struct gets a same such as <code>(anonymous struct at file.zig:7:38)</code>.</li> + </ul> + {#code_begin|exe|struct_name#} +const std = @import("std"); + +pub fn main() void { + const Foo = struct {}; + std.debug.warn("variable: {}\n", @typeName(Foo)); + std.debug.warn("anonymous: {}\n", @typeName(struct {})); + std.debug.warn("function: {}\n", @typeName(List(i32))); +} + +fn List(comptime T: type) type { + return struct { + x: T, + }; +} + {#code_end#} + {#header_close#} {#see_also|comptime|@fieldParentPtr#} {#header_close#} {#header_open|enum#} |
