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).
reflection
Compile-time reflection utilities for introspecting Mojo types and functions.
This module provides compile-time reflection capabilities including:
- Unified type and struct reflection via
reflect[T]()returning aReflected[T]handle (user.name()andr.base_name()for type names) - Function name and linkage introspection (
get_function_name,get_linkage_name) - Source location introspection (
source_location,call_location) - Deprecated free functions:
get_type_name(usereflect[T]().name()) andget_base_type_name(usereflect[T]().base_name())
reflect is auto-imported via the prelude. The other names listed above
must be imported explicitly from std.reflection.
Example:
struct Point:
var x: Int
var y: Float64
def print_fields[T: AnyType]():
comptime r = reflect[T]()
comptime names = r.field_names()
comptime for i in range(r.field_count()):
print(names[i])
def main():
print_fields[Point]() # Prints: x, y
Modules
-
location: Implements utilities to capture and represent source code location. -
reflect: Provides the unifiedreflect[T]/Reflected[T]reflection API. -
struct_fields: Deprecated free-function reflection API. -
traits: Compile-time meta functions for checking trait conformance across variadic type lists. -
type_info: Provides type and function name introspection utilities.