@register_passable
All the arguments and variables of this decorator should adhere to types that should be passed in machine registers (like CPU registers). This mean all these types should always be passed by value and cannot be passed by reference. These structs cannot hold values that are not register passable.
Types:
- @register_passable
- should implement
__init__,__copyinit__,__del__and no move.
- should implement
- @register_passable(“trivial”)
- can only define
__init__not mandatory, other are only defined by compiler. - Arithmetic types such as
Int,Bool,Float64etc. - Pointers (the address value is trivial, not the data being pointed to).
- Arrays of other trivial types, including SIMD.
- can only define
@fieldwise_init
It tells the compiler to automatically write the constructor __init__ for your struct
@implicit
Mojo implicitly converts type if this decorator is used.
struct Target:
@implicit
fn __init__(out self, s: Source): ...Mojo implicitly converts from source to target if needed.
b:Source = ...
a:Target = b # is valid, due to implicit conversion@fieldwise_init(“implicit”)
it allows the struct to be implicitly converted from its fields
@always_inline
Mojo compiler copies the body of this function to body of the function where its being called. This avoids performance costs associated with function calls, but it increases binary size.
@always_inline(‘nodebug’)
Same as above but without debug info, debugger cannot step into this function when debugging, should be used with low level code.
@value
Generates boilerplate lifecycle methods. __init__, __copyinit__, __moveinit__,__del__
@parameter
- Over If condition - will be evaluated at compile time. If condition should be valid parameter expression
- Over for loop - will be evaluated at compile time, loop sequence and induction values must be valid parameter expressions.
- Closure function - over nested function
- Closure function can capture variables or parameter from outer scope
- Use the closure function as a parameter.
Though closure function contains dynamic values from outer loop, it can still be accessed as parameter.
@__copy_capture(variable_name)
Passed on top of parameter closure to capture register passable values by copy. This copies the mentioned variable in to closure function by value rather capturing it by reference. This avoids any lifetime concerns regarding that variable.