Runtime Debug Introspection
When both structs and enums carry @reflect, a single generic loop can dump any
reflected value's name, kind, and field types without knowing the concrete type
at write time. This enables zero-boilerplate debug utilities and universal
serializers that work across the entire type system.
Use @reflect on both a struct and an enum, then call typeinfo(u) at runtime to dump field names/types and confirm an enum's variant count — all without knowing the concrete type at write time.
10-debug-introspect.zolo
// Use case 7 — DEBUG / RUNTIME INTROSPECTION
//
// With `@reflect`, a value carries its type descriptor at runtime. `typeinfo(v)`
// returns it (via the value's metatable), so a generic debug dumper can report
// any reflected value's shape while the program runs — no per-type code.
@reflect
struct User { id: int, name: str, active: bool }
@reflect
enum Status { Active(int), Banned, Pending }
let u = User.new(7, "ada", true)
let info = typeinfo(u)
print("type: " + info.name + " (" + info.kind + ")")
// expected: type: User (struct)
var dump = ""
for f in info.fields {
dump = dump + " ." + f.name + ": " + f.type_name + "\n"
}
print(dump)
// expected:
// .id: int
// .name: str
// .active: bool
// Enums reflect too: inspect a live variant's enum type.
let s = Status.Active(3)
let sinfo = typeinfo(s)
print("enum: " + sinfo.name + " with " + tostring(sinfo.variants.len()) + " variants")
// expected: enum: Status with 3 variants
See also