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: Nightly
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], a comptime alias for the Reflected[T] handle type. Use reflect[T].name(), reflect[T].base_name(), reflect[T].field_count(), etc.
  • Function name and linkage introspection (get_function_name, get_linkage_name).
  • Source location introspection (source_location, call_location).

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 names = reflect[T].field_names()
comptime for i in range(reflect[T].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.
  • traits: Compile-time meta functions for checking trait conformance across variadic type lists.
  • type_info: Provides function name introspection utilities.