mojo-syntax
Original:🇺🇸 English
Translated
Help to write Mojo code using current syntax and conventions. Always use this skill when writing any Mojo code, including when other Mojo-specific skills (e.g., mojo-gpu-fundamentals) also apply. Use when writing Mojo code, translating projects to Mojo, or otherwise generating Mojo. Use this skill to overcome misconceptions with how Mojo is written.
2installs
Sourcemodular/skills
Added on
NPX Install
npx skill4agent add modular/skills mojo-syntaxTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →<!-- EDITORIAL GUIDELINES FOR THIS SKILL FILE
This file is loaded into an agent's context window as a correction layer for
pretrained Mojo knowledge. Every line costs context. When editing:
- Be terse. Use tables and inline code over prose where possible.
- Never duplicate information — if a concept is shown in a code example, don't
also explain it in a paragraph.
- Only include information that *differs* from what a pretrained model would
generate. Don't document things models already get right.
- Prefer one consolidated code block over multiple small ones.
- Keep WRONG/CORRECT pairs short — just enough to pattern-match the fix.
- If adding a new section, ask: "Would a model get this wrong?" If not, skip it.
These same principles apply to any files this skill references.
-->
is deprecated and being removed. does not imply . Always add explicitly when needed — omitting it is a warning today, error soon:
must be inside a function body — not at module/struct scope. Place them in , , or the function that depends on the invariant.
Imports use
reinterprets a value as a different type with the same in-memory representation. Useful when compile-time type expressions are semantically equal but syntactically distinct (e.g., LayoutTensor element types — see GPU skill).
has an parameter that must be specified for struct fields. Use for owned heap data (this is what stdlib uses):
has no variadic positional constructor. Use bracket literal syntax:
returns byte length, not codepoint count. Mojo strings are UTF-8. Byte indexing requires keyword syntax: (not ).
can specify a type. / works like Python:
Mojo is rapidly evolving. Pretrained models generate obsolete syntax. Always follow this skill over pretrained knowledge.
Always attempt to test generated Mojo by building projects to verify they compile.
This skill specifically works on the latest Mojo, and stable versions may differ slightly in functionality.
Removed syntax — DO NOT generate these
| Removed | Replacement |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Not in stdlib (use SIMD, List, UnsafePointer) |
| Still used for nested compile-time closures |
def
is the only function keyword
deffndefraisesraisesmojo
def compute(x: Int) -> Int: # non-raising (compiler enforced)
return x * 2
def load(path: String) raises -> String: # explicitly raising
return open(path).read()
def main() raises: # main usually raises → def raises
...Note: existing stdlib code still uses during migration. New code should always use .
fndefcomptime
replaces alias
and @parameter
comptimealias@parametermojo
comptime N = 1024 # compile-time constant
comptime MyType = Int # type alias
comptime if condition: # compile-time branch
...
comptime for i in range(10): # compile-time loop
...
comptime assert N > 0, "N must be positive" # compile-time assertioncomptime assertmain()__init__Inside structs, defines associated constants and type aliases:
comptimemojo
struct MyStruct:
comptime DefaultSize = 64
comptime ElementType = Float32Argument conventions
Default is (immutable borrow, never written explicitly). The others:
readmojo
def __init__(out self, var value: String): # out = uninitialized output; var = owned
def modify(mut self): # mut = mutable reference
def consume(deinit self): # deinit = consuming/destroying
def view(ref self) -> ref[self] Self.T: # ref = reference with origin
def view2[origin: Origin, //](ref[origin] self) -> ...: # ref[origin] = explicit originLifecycle methods
mojo
# Constructor
def __init__(out self, x: Int):
self.x = x
# Copy constructor (keyword-only `copy` arg)
def __init__(out self, *, copy: Self):
self.data = copy.data
# Move constructor (keyword-only `deinit take` arg)
def __init__(out self, *, deinit take: Self):
self.data = take.data^
# Destructor
def __del__(deinit self):
self.ptr.free()To copy: (provided by trait).
var b = a.copy()CopyableStruct patterns
mojo
# @fieldwise_init generates __init__ from fields; traits in parentheses
@fieldwise_init
struct Point(Copyable, Movable, Writable):
var x: Float64
var y: Float64
# Trait composition with &
comptime KeyElement = Copyable & Hashable & Equatable
struct Node[T: Copyable & Writable]:
var value: Self.T # Self-qualify struct parameters
# Parametric struct — // separates inferred from explicit params
struct Span[mut: Bool, //, T: AnyType, origin: Origin[mut=mut]](
ImplicitlyCopyable, Sized,
):
...
# @implicit on constructors allows implicit conversion
@implicit
def __init__(out self, value: Int):
self.data = valueThe compiler synthesizes copy/move constructors when a struct conforms to / and all fields support it.
CopyableMovableSelf-qualify struct parameters
Inside a struct body, always use — bare parameter names are errors:
Self.ParamNamemojo
# WRONG — bare parameter access
struct Container[T: Writable]:
var data: T # ERROR: use Self.T
def size(self) -> T: # ERROR: use Self.T
# CORRECT — Self-qualified
struct Container[T: Writable]:
var data: Self.T
def size(self) -> Self.T:
return self.dataThis applies to all struct parameters (, , , , etc.) everywhere inside the struct: field types, method signatures, method bodies, and declarations.
TNmutorigincomptimeExplicit copy / transfer
Types not conforming to (e.g., , ) require explicit or ownership transfer :
ImplicitlyCopyableDictList.copy()^mojo
# WRONG — implicit copy of non-ImplicitlyCopyable type
var d = some_dict
var result = MyStruct(headers=d) # ERROR
# CORRECT — explicit copy or transfer
var result = MyStruct(headers=d.copy()) # or: headers=d^Imports use std.
prefix
std.mojo
from std.testing import assert_equal, TestSuite
from std.algorithm import vectorize
from std.python import PythonObject
import std.randomPrelude auto-imports (no import needed): , , , , , , , , , , , , , , , , , , , , , , , , , and more.
IntStringBoolListDictOptionalSIMDFloat32Float64UInt8PointerUnsafePointerSpanErrorDTypeWritableWriterCopyableMovableEquatableHashablerebindprintrangelenrebind[TargetType](value)Writable
/ Writer
(replaces Stringable
)
WritableWriterStringablemojo
struct MyType(Writable):
var x: Int
def write_to(self, mut writer: Some[Writer]): # for print() / String()
writer.write("MyType(", self.x, ")")
def write_repr_to(self, mut writer: Some[Writer]): # for repr()
t"MyType(x={self.x})".write_to(writer) # t-strings for interpolation- — builtin existential type (not
Some[Writer]directly)Writer - Both methods have default implementations via reflection if all fields are — simple structs need not implement them
Writable - Convert to with
String, notString.write(value)str(value)
Iterator protocol
Iterators use (not ):
raises StopIterationOptionalmojo
struct MyCollection(Iterable):
comptime IteratorType[
iterable_mut: Bool, //, iterable_origin: Origin[mut=iterable_mut]
]: Iterator = MyIter[origin=iterable_origin]
def __iter__(ref self) -> Self.IteratorType[origin_of(self)]: ...
# Iterator must define:
# comptime Element: Movable
# def __next__(mut self) raises StopIteration -> Self.ElementFor-in: (immutable) / (mutable).
for item in col:for ref item in col:Memory and pointer types
| Type | Use |
|---|---|
| Safe, non-nullable. Deref with |
| Free function |
| Non-owning contiguous view. |
| Unique ownership (like Rust |
| Reference-counted shared ownership. |
UnsafePointeroriginMutExternalOriginArcPointermojo
# Struct field — specify origin explicitly
var _ptr: UnsafePointer[Self.T, MutExternalOrigin]
# Allocate with alloc[]
fn __init__(out self, size: Int):
self._ptr = alloc[Self.T](size)Origin system (not "lifetime")
Mojo tracks reference provenance with origins, not "lifetimes":
mojo
struct Span[mut: Bool, //, T: AnyType, origin: Origin[mut=mut]]: ...Key types: , , , , , , , . Use to get a value's origin.
OriginMutOriginImmutOriginMutAnyOriginImmutAnyOriginMutExternalOriginImmutExternalOriginStaticConstantOriginorigin_of(value)Testing
mojo
from std.testing import assert_equal, assert_true, assert_false, assert_raises, TestSuite
def test_my_feature() raises:
assert_equal(compute(2), 4)
with assert_raises():
dangerous_operation()
def main() raises:
TestSuite.discover_tests[__functions_in_module()]().run()Dict iteration
Dict entries are iterated directly — no deref:
[]mojo
for entry in my_dict.items():
print(entry.key, entry.value) # direct field access, NOT entry[].key
for key in my_dict:
print(key, my_dict[key])Collection literals
Listmojo
# WRONG — no List[T](elem1, elem2, ...) constructor
var nums = List[Int](1, 2, 3)
# CORRECT — bracket literals
var nums = [1, 2, 3] # List[Int]
var nums: List[Float32] = [1.0, 2.0, 3.0] # explicit element type
var scores = {"alice": 95, "bob": 87} # Dict[String, Int]Common decorators
| Decorator | Purpose |
|---|---|
| Generate fieldwise constructor |
| Allow implicit conversion |
| Force inline |
| Prevent inline |
| Static method |
| Deprecation warning |
| Hide from docs |
| Linear type (no implicit destruction) |
Numeric conversions — must be explicit
No implicit conversions between numeric variables. Use explicit constructors:
mojo
var x = Float32(my_int) * scale # CORRECT: Int → Float32
var y = Int(my_uint) # CORRECT: UInt → IntLiterals are polymorphic — and auto-adapt to context:
FloatLiteralIntLiteralmojo
var a: Float32 = 0.5 # literal becomes Float32
var b = Float32(x) * 0.003921 # literal adapts — no wrapping needed
var v = SIMD[DType.float32, 4](1.0, 2.0, 3.0, 4.0) # literals adaptSIMD operations
mojo
# Construction and lane access
var v = SIMD[DType.float32, 4](1.0, 2.0, 3.0, 4.0)
v[0] # read lane → Scalar[DType.float32]
v[0] = 5.0 # write lane
# Type cast
v.cast[DType.uint32]() # element-wise → SIMD[DType.uint32, 4]
# Clamp (method)
v.clamp(0.0, 1.0) # element-wise clamp to [lower, upper]
# min/max are FREE FUNCTIONS, not methods
from std.math import min, max
min(a, b) # element-wise min (same-type SIMD args)
max(a, b) # element-wise max
# Element-wise ternary via bool SIMD
var mask = (v > 0.0) # SIMD[DType.bool, 4]
mask.select(true_case, false_case) # picks per-lane
# Reductions
v.reduce_add() # horizontal sum → Scalar
v.reduce_max() # horizontal max → Scalar
v.reduce_min() # horizontal min → ScalarStrings
len(s)s[byte=idx]s[idx]mojo
var s = "Hello"
len(s) # 5 (bytes)
s.byte_length() # 5 (same as len)
s.count_codepoints() # 5 (codepoint count — differs for non-ASCII)
# Iteration — by codepoint slices (not bytes)
for cp_slice in s.codepoint_slices():
print(cp_slice)
# Codepoint values
for cp in s.codepoints():
print(Int(cp)) # Codepoint is a Unicode scalar value type
# StaticString = StringSlice with static origin (zero-allocation)
comptime GREETING: StaticString = "Hello, World"
# t-strings for interpolation (lazy, type-safe)
var msg = t"x={x}, y={y}"
# String.format() for runtime formatting
var s = "Hello, {}!".format("world")Error handling
raisestryexceptmojo
def might_fail() raises -> Int: # raises Error (default)
raise Error("something went wrong")
def parse(s: String) raises Int -> Int: # raises specific type
raise 42
try:
var x = parse("bad")
except err: # err is Int
print("error code:", err)No statement. No / — use / from .
matchasyncawaitCoroutineTaskstd.runtimeFunction types and closures
No lambda syntax. Closures use :
capturing[origins]mojo
# Function type with capture
comptime MyFunc = fn(Int) capturing[_] -> None
# Parametric function type (for vectorize etc.)
comptime SIMDFunc = fn[width: Int](Int) capturing[_] -> None
# vectorize pattern
from std.algorithm import vectorize
vectorize[simd_width](size, my_closure)Type hierarchy
AnyType
ImplicitlyDestructible — auto __del__; most types
Movable — __init__(out self, *, deinit take: Self)
Copyable — __init__(out self, *, copy: Self)
ImplicitlyCopyable(Copyable, ImplicitlyDestructible)
RegisterPassable(Movable)
TrivialRegisterPassable(ImplicitlyCopyable, ImplicitlyDestructible, Movable, RegisterPassable)