Skip to main content

Mojo decorators

A Mojo decorator is a higher-order function that modifies or extends the behavior of a struct, a function, or some other code. Instead of actually calling the higher-order function, you simply add the decorator (such as the @value decorator) above your code (such as a struct). The Mojo compiler then uses the decorator function to modify your code at compile time.

No custom decorators

The creation of custom decorators is not yet supported. The available ones are built directly into the compiler.

The following pages describe each built-in decorator with examples.

@always_inline

Copies the body of a function directly into the body of the calling function.

@__copy_capture

Captures register-passable typed values by copy.

@nonmaterializable

Declares that a type should exist only in the parameter domain.

@parameter

Executes a function or if statement at compile time.

@register_passable

Declares that a type should be passed in machine registers.

@staticmethod

Declares a struct method as static.

@value

Generates boilerplate lifecycle methods for a struct.