Version: 1.1.0

Reflection

Compile-time reflection inspects a type's fields and methods so generic code can be written once.

Essentials

  • reflect[T]() describes the type parameter.
  • Field names and field types are themselves compile-time values.
  • A comptime for walks the fields.
  • Each access still type-checks, so a reflected field must meet the trait you use.
  • Reflection does not look through private implementation details you did not declare.

Example

fn dump_fields[T: AnyType]():
    comptime r = reflect[T]()
    comptime:
        for i in range(r.field_names().__len__()):
            print(r.field_names()[i])

Keep in mind

Reflection is for libraries and equality helpers, not for hiding a messy data model.