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.
ποΈ @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.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!