@always_inline
: decorator on any function to make the Mojo compiler "inline" the body of the function (copy it) directly into the body of the calling function.
@parameter
: decorator on an if statement or on a nested function to run that code at compile time.
@register_passable
: decorator on a struct to tell Mojo that the type should be passed in machine registers (such as a CPU register; subject to the details of the underlying architecture).
@value
: decorator on a struct to generate boilerplate lifecycle methods, including the member-wise init() constructor, copyinit() copy constructor, and moveinit() move constructor.
-
To define positional-only arguments, add a slash character (
/
) to the argument list. Any arguments before the/
are positional-only: they can't be passed as keyword arguments. -
If the function doesn't accept variadic arguments, you can add a single star (*) to the argument list to separate the keyword-only arguments.
-
Variadic arguments look like:
fn sum(*values: Int) -> Int:
-
Also, any arguments declared after the variadic argument can only be specified by keyword.
StaticTuple
: A statically sized tuple type which contains elements of homogeneous types. To use, from utils import static_tuple
.
DType
is a type that names other types, e.g., var x: DType = DType.uint64
. Useful in generics.
This
trait MyTrait[T: TypeClass]:
...
is not allowed. However, this
trait MyTrait:
fn passthrough[T: TypeClass](t: T) -> T:
pass
...
is allowed. The trick is that a type fulfilling the trait must also be generic. Specialization only occurs at the point of invocation, not in the type definition. So
struct MyStruct(MyTrait):
fn passthrough(t: AType) -> AType:
...
is not allowed. But
struct MyStruct(MyTrait):
fn passthrough[T: TypeClass](t: T) -> T:
return t
var s = MyStruct()
var t = SomeType()
var res: SomeType = s.passthrough(t)
is allowed. Specialization occurs in the last line.