Compile-Time Serialization Template
By iterating typeinfo(T).fields inside a comptime block and reading each
field's .name and .type.kind, you can fold a type's entire shape into a
constant string — a JSON template, a codec skeleton, or a schema fragment —
all computed once at compile time with no runtime overhead.
Iterate typeinfo(User).fields in a comptime block to fold the struct shape into a constant JSON-template string like {"id": <int>, "name": <str>, "active": <bool>}.
04-serialization.zolo
// Use case 1 — SERIALIZATION / CODEC
//
// Read a type's shape at compile time and generate a JSON-object template
// from its fields. A real codec walks the same `typeinfo(T).fields` to encode
// each value; here we fold the *shape* into a constant string at zero runtime
// cost. The `type.kind` tag drives how each field would be encoded.
struct User {
id: int,
name: str,
active: bool,
}
let json_template = comptime {
var s = "\{"
var first = true
for f in typeinfo(User).fields {
if first == false { s = s + ", " }
s = s + "\"" + f.name + "\": <" + f.type.kind + ">"
first = false
}
s = s + "\}"
s
}
print(json_template)
// expected: {"id": <int>, "name": <str>, "active": <bool>}