IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

@__copy_capture

You can add the __copy_capture decorator on a parametric closure to capture register-passable values by copy. This decorator causes a nested function to copy the value of the indicated variable into the closure object at the point of formation instead of capturing that variable by reference. This allows the closure to be passed as an escaping function, without lifetime concerns.

def foo(x: Int):
var z = x

@__copy_capture(z)
@parameter
def formatter() -> Int:
return z
z = 2
print(formatter())

def main():
foo(5)