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 @fieldwise_init decorator) above your code (such as a struct). The Mojo
compiler then uses the decorator function to modify your code at compile time.
The following pages describe each built-in decorator with examples.
📄️ @align
Specifies a minimum alignment for a struct.
📄️ @always_inline
Copies the body of a function directly into the body of the calling function.
📄️ @compiler.register
Registers a custom operation for use with the MAX Graph API.
📄️ @__copy_capture
Captures register-passable typed values by copy.
📄️ @deprecated
Mojo's `@deprecated` decorator marks outdated APIs and schedules them for removal. When used with the `use` parameter, it also provides migration suggestions.
📄️ @doc_hidden
Hides declarations from generated documentation.
📄️ @explicit_destroy
Prevents automatic destruction by a `__del__()` method and requires explicit cleanup through named destructor methods.
📄️ @export
Marks a function for export.
📄️ @fieldwise_init
Generates fieldwise constructor for a struct.
📄️ @implicit
Marks a constructor as eligible for implicit conversion.
📄️ @no_inline
Prevents a function from being inlined.
📄️ @parameter
Executes a function or if statement at compile time.
📄️ @staticmethod
Declares a struct method as static.