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).
Mojo decorators
A Mojo decorator modifies or extends the behavior of a struct, function, or
other declaration at compile time. You place the decorator on the line
above the declaration it applies to, prefixed with @.
@fieldwise_init
struct Point:
var x: Float64
var y: Float64
After the @, a decorator is followed by a name, with optional arguments
in parentheses. Each decorator goes on its own line. You can stack multiple
decorators on a single declaration:
@fieldwise_init
@align(64)
struct CacheLine:
var data: SIMD[DType.float32, 16]
Decorators apply bottom-up: the one closest to the declaration is applied first.
Decorators
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.
Decorator targets
Not every decorator works on every declaration. This table shows which decorators are valid on which targets.
| Decorator | struct | def | method | trait | comptime | var | field |
|---|---|---|---|---|---|---|---|
@align | yes | ||||||
@always_inline | yes | yes | |||||
@compiler.register | yes | ||||||
@__copy_capture | yes | ||||||
@deprecated | yes | yes | yes | yes | yes | ||
@doc_hidden | yes | yes | yes | yes | yes | ||
@explicit_destroy | yes | ||||||
@export | yes | ||||||
@fieldwise_init | yes | ||||||
@implicit | yes | ||||||
@no_inline | yes | yes | |||||
@nonmaterializable | yes | ||||||
@parameter | yes | ||||||
@staticmethod | yes |