From 780e5674467ebac4534cd3d3f2199ccaf1d0922c Mon Sep 17 00:00:00 2001
From: Andrew Kelley
{#syntax#}@This() type{#endsyntax#}
+ + 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: +
+ {#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#} ++ 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 + #1047 for details. +
+ {#header_close#} {#header_open|@truncate#}{#syntax#}@truncate(comptime T: type, integer) T{#endsyntax#}