Skip to content

Latest commit

 

History

History
65 lines (44 loc) · 2.87 KB

mojo_notes.md

File metadata and controls

65 lines (44 loc) · 2.87 KB

Mojo notes

Dectorators

@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.

Function arguments

Containers / arrays

StaticTuple: A statically sized tuple type which contains elements of homogeneous types. To use, from utils import static_tuple.

Types

DType is a type that names other types, e.g., var x: DType = DType.uint64. Useful in generics.

Generic trait members

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.