struct_fields
Provides struct field reflection and introspection utilities.
This module provides compile-time introspection of struct fields:
struct_field_count[T]()- returns the number of fieldsstruct_field_names[T]()- returns anInlineArray[StaticString, N]of field namesstruct_field_types[T]()- returns a variadic of field types
These APIs work with both concrete types and generic type parameters, enabling generic serialization, comparison, and other reflection-based utilities.
For field lookup by name (concrete types only):
struct_field_index_by_name[T, name]()- returns the index of a field by namestruct_field_type_by_name[T, name]()- returns the type of a field by name
Example iterating over all fields (works with generics):
from std.reflection import struct_field_names, struct_field_count
struct Point:
var x: Int
var y: Float64
def print_fields[T: AnyType]():
comptime names = struct_field_names[T]()
comptime for i in range(struct_field_count[T]()):
print(names[i])
def main():
print_fields[Point]() # Works with any struct!
Example looking up a field by name:
from std.reflection import (
struct_field_index_by_name,
struct_field_type_by_name,
)
struct Point:
var x: Int
var y: Float64
def main():
comptime idx = struct_field_index_by_name[Point, "x"]() # 0
comptime field_type = struct_field_type_by_name[Point, "y"]()
var value: field_type.T = 3.14 # field_type.T is Float64
For accessing struct field values by index (returns a reference, not a copy):
__struct_field_ref(idx, ref s)- returns a reference to the field at index
The __struct_field_ref magic function enables reflection-based utilities to work
with non-copyable types by returning references instead of copies. It works with
both literal indices and parametric indices (such as loop variables in
comptime for loops):
from std.reflection import struct_field_names, struct_field_count
@fieldwise_init
struct Container:
var id: Int
var name: String
def inspect(mut c: Container):
# Get references to fields without copying
ref id_ref = __struct_field_ref(0, c)
ref name_ref = __struct_field_ref(1, c)
# Mutation through reference
__struct_field_ref(0, c) = 42
def main():
var c = Container(id=1, name="test")
inspect(c)
For struct field byte offsets (useful for low-level memory operations):
offset_of[T, name="field_name"]()- get offset by field nameoffset_of[T, index=0]()- get offset by field index
Example:
from std.reflection import offset_of
struct Point:
var x: Int # offset 0
var y: Float64 # offset 8 (aligned to 8 bytes)
def main():
comptime x_off = offset_of[Point, name="x"]() # 0
comptime y_off = offset_of[Point, index=1]() # 8
comptime values
struct_field_types
comptime struct_field_types[T: AnyType] = TypeList[#kgen.struct_field_types<:trait<@std::@builtin::@anytype::@AnyType> T>]
Returns the types of all fields in struct T as a TypeList.
This function works with both concrete types and generic type parameters.
For nested structs, this returns the struct type itself, not its flattened fields. Use recursive calls to introspect nested types.
Returns: A list of types, one for each field in the struct.
Example:
from std.reflection import get_type_name, struct_field_types, struct_field_count
struct MyStruct:
var x: Int
var y: Float64
def print_field_types[T: AnyType]():
comptime types = struct_field_types[T]()
comptime for i in range(struct_field_count[T]()):
print(get_type_name[types[i]]())
def main():
print_field_types[MyStruct]() # Works with any struct!
Parameters
- T (
AnyType): A struct type.
Structs
-
ReflectedType: Wrapper struct for compile-time type values from reflection.
Functions
-
is_struct_type: ReturnsTrueifTis a Mojo struct type,Falseotherwise. -
offset_of: Returns the byte offset of a field within a struct by name. -
struct_field_count: Returns the number of fields in structT. -
struct_field_index_by_name: Returns the index of the field with the given name in structT. -
struct_field_names: Returns the names of all fields in structTas an InlineArray. -
struct_field_type_by_name: Returns the type of the field with the given name in structStructT.