Mojo package
reflection
Compile-time reflection utilities for introspecting Mojo types and functions.
This module provides compile-time reflection capabilities including:
- Type name introspection (
get_type_name) - Function name and linkage introspection (
get_function_name,get_linkage_name) - Struct field reflection (
struct_field_count,struct_field_names,struct_field_types) - Field lookup by name (
struct_field_index_by_name,struct_field_type_by_name)
Example:
from reflection import struct_field_count, struct_field_names
struct Point:
var x: Int
var y: Float64
fn print_fields[T: AnyType]():
comptime names = struct_field_names[T]()
@parameter
for i in range(struct_field_count[T]()):
print(names[i])
fn main():
print_fields[Point]() # Prints: x, yModules
-
reflection: Provides utilities for compile-time reflection and type introspection.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!