Skip to content

16 · Reflection & Metaprogramming

Runtime Reflection

Opt a struct into runtime type metadata with @reflect, then query it via typeinfo(value).

On this page

Types annotated with @reflect carry a lightweight metadata table at runtime. Passing an instance to typeinfo() returns the same field descriptor as the comptime form, but resolved on a live value — useful for generic serializers and debuggers that must work without knowing the type at compile time.

Annotate a User struct with @reflect, then call typeinfo(u) on a live instance to read its name and field type at runtime.

02-runtime-reflect.zolo
Playground
// Feature: opt into runtime reflection with `@reflect`, then call

// `typeinfo(value)` to inspect an instance's type while the program runs.

// Types without `@reflect` carry no metadata (zero cost).


@reflect
@entity(table: "users")
struct User {
  @column(name: "user_id", primary: true)
  id: int,
  name: str,
}

let u = User.new(7, "ada")
let info = typeinfo(u)
print(info.name)
// expected: User

print(info.fields[1].type_name)
// expected: str

print(info.decorators[1].name + " " + info.decorators[1].named.table)
// expected: entity users

print(info.fields[0].decorators[0].named.name)
// expected: user_id

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

en