| ... | @@ -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#} |
| | 6163 | const std = @import("std"); |
| | 6164 | const assert = std.debug.assert; |
| | 6165 | |
| | 6166 | test "@This()" { |
| | 6167 | var items = []i32{ 1, 2, 3, 4 }; |
| | 6168 | const list = List(i32){ .items = items[0..] }; |
| | 6169 | assert(list.length() == 4); |
| | 6170 | } |
| | 6171 | |
| | 6172 | fn 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> |