diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-09-15 10:14:50 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-09-15 10:14:50 -0400 |
| commit | 780e5674467ebac4534cd3d3f2199ccaf1d0922c (patch) | |
| tree | 1c06edc18c3a2e16f5dba94c1fde90f742cea3fe /doc | |
| parent | 3f776af3fa58b47d15a81b17c15e5b0e1c5ccf28 (diff) | |
| download | zig-780e5674467ebac4534cd3d3f2199ccaf1d0922c.tar.gz zig-780e5674467ebac4534cd3d3f2199ccaf1d0922c.zip | |
add docs for `@This()`
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/langref.html.in | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in index 4e39787ede..f5b92873e0 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -6153,6 +6153,41 @@ pub const FloatMode = enum { For a union, returns the enum type that is used to store the tag value. </p> {#header_close#} + {#header_open|@This#} + <pre>{#syntax#}@This() type{#endsyntax#}</pre> + <p> + Returns the innermost struct or union that this function call is inside. + This can be useful for an anonymous struct that needs to refer to itself: + </p> + {#code_begin|test#} +const std = @import("std"); +const assert = std.debug.assert; + +test "@This()" { + var items = []i32{ 1, 2, 3, 4 }; + const list = List(i32){ .items = items[0..] }; + assert(list.length() == 4); +} + +fn List(comptime T: type) type { + return struct { + const Self = @This(); + + items: []T, + + fn length(self: Self) usize { + return self.items.len; + } + }; +} + {#code_end#} + <p> + When {#syntax#}@This(){#endsyntax#} is used at global scope, it returns a reference to the + current import. There is a proposal to remove the import type and use an empty struct + type instead. See + <a href="https://github.com/ziglang/zig/issues/1047">#1047</a> for details. + </p> + {#header_close#} {#header_open|@truncate#} <pre>{#syntax#}@truncate(comptime T: type, integer) T{#endsyntax#}</pre> <p> |
