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).

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 a Reflected[T] handle (use r.name() and r.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 (use reflect[T]().name()) and get_base_type_name (use reflect[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 unified reflect[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.