IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

struct_fields

Deprecated free-function reflection API.

The free functions and ReflectedType[T] wrapper in this module are deprecated in favor of the unified reflect[T]() -> Reflected[T] API. New code should use reflect[T]() from std.reflection directly:

DeprecatedReplacement
struct_field_count[T]()reflect[T]().field_count()
struct_field_names[T]()reflect[T]().field_names()
struct_field_types[T]()reflect[T]().field_types()
struct_field_index_by_name[T, name]()reflect[T]().field_index[name]()
struct_field_type_by_name[T, name]()reflect[T]().field_type[name]()
struct_field_ref[idx](s)reflect[T]().field_ref[idx](s)
is_struct_type[T]()reflect[T]().is_struct()
offset_of[T, name=...]()reflect[T]().field_offset[name=...]()
offset_of[T, index=...]()reflect[T]().field_offset[index=...]()
ReflectedType[T]Reflected[T]

Each wrapper here delegates to the new API; they remain to give external callers time to migrate and will be removed in a future release.

comptime values

struct_field_types

comptime struct_field_types[T: AnyType] = TypeList[#kgen.struct_field_types<:trait<@std::@builtin::@anytype::@AnyType> T>]

Deprecated: use reflect[T]().field_types() instead.

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!

Deprecated: Use reflect[T]().field_types() instead.

Parameters

Structs

Functions