authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-15 10:14:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-15 10:14:50-04:00
log780e5674467ebac4534cd3d3f2199ccaf1d0922c
tree1c06edc18c3a2e16f5dba94c1fde90f742cea3fe
parent3f776af3fa58b47d15a81b17c15e5b0e1c5ccf28
signaturelock-open Commit is signed but in an unrecognized format.

add docs for `@This()`


1 files changed, 35 insertions(+), 0 deletions(-)

doc/langref.html.in+35
...@@ -6153,6 +6153,41 @@ pub const FloatMode = enum {...@@ -6153,6 +6153,41 @@ pub const FloatMode = enum {
6153 For a union, returns the enum type that is used to store the tag value.6153 For a union, returns the enum type that is used to store the tag value.
6154 </p>6154 </p>
6155 {#header_close#}6155 {#header_close#}
6156 {#header_open|@This#}
6157 <pre>{#syntax#}@This() type{#endsyntax#}</pre>
6158 <p>
6159 Returns the innermost struct or union that this function call is inside.
6160 This can be useful for an anonymous struct that needs to refer to itself:
6161 </p>
6162 {#code_begin|test#}
6163const std = @import("std");
6164const assert = std.debug.assert;
6165
6166test "@This()" {
6167 var items = []i32{ 1, 2, 3, 4 };
6168 const list = List(i32){ .items = items[0..] };
6169 assert(list.length() == 4);
6170}
6171
6172fn List(comptime T: type) type {
6173 return struct {
6174 const Self = @This();
6175
6176 items: []T,
6177
6178 fn length(self: Self) usize {
6179 return self.items.len;
6180 }
6181 };
6182}
6183 {#code_end#}
6184 <p>
6185 When {#syntax#}@This(){#endsyntax#} is used at global scope, it returns a reference to the
6186 current import. There is a proposal to remove the import type and use an empty struct
6187 type instead. See
6188 <a href="https://github.com/ziglang/zig/issues/1047">#1047</a> for details.
6189 </p>
6190 {#header_close#}
6156 {#header_open|@truncate#}6191 {#header_open|@truncate#}
6157 <pre>{#syntax#}@truncate(comptime T: type, integer) T{#endsyntax#}</pre>6192 <pre>{#syntax#}@truncate(comptime T: type, integer) T{#endsyntax#}</pre>
6158 <p>6193 <p>