Files
sx/examples/0616-comptime-field-type.sx
agra bd139dc09c xfail(reify): field_type — read struct/enum member types by index
REIFY Phase 2.0. Add examples/0616: reflect a struct's fields (name via
field_name, type via field_type) and a tagged-union's variant payloads,
including field_type composed inside type_eq / type_name. Seed an empty
expected/*.exit marker. RED by design — field_type still bails ("not yet
implemented"); Phase 2.1 implements it over the type table and turns
this green.
2026-06-16 19:01:03 +03:00

30 lines
1.4 KiB
Plaintext

// REIFY Phase 2: `field_type($T, i) -> Type` — the type-only field projection
// the value-level reflection (`field_value` / `type_of`) couldn't express.
// Reflect a struct's fields by name (`field_name`) AND by type (`field_type`),
// and a tagged-union's variant payloads. It folds at lower time, so it composes
// inside `type_eq` / `type_name` / any type-arg slot.
#import "modules/std.sx";
#import "modules/std/meta.sx";
Point :: struct { x: i64; y: f64; on: bool; }
Msg :: enum { num: i64; tag: bool; quit; }
main :: () -> i32 {
// Struct fields: name + type, position by position.
print("Point has {} fields\n", field_count(Point));
print(" 0: {} : {}\n", field_name(Point, 0), type_name(field_type(Point, 0)));
print(" 1: {} : {}\n", field_name(Point, 1), type_name(field_type(Point, 1)));
print(" 2: {} : {}\n", field_name(Point, 2), type_name(field_type(Point, 2)));
// field_type composes in a type-arg slot (type_eq folds at comptime).
print("field 0 is i64: {}\n", type_eq(field_type(Point, 0), i64));
print("field 1 is f64: {}\n", type_eq(field_type(Point, 1), f64));
// Tagged-union variant payloads (the tagless `quit` → void).
print("Msg.num payload: {}\n", type_name(field_type(Msg, 0)));
print("Msg.tag payload: {}\n", type_name(field_type(Msg, 1)));
print("Msg.quit payload: {}\n", type_name(field_type(Msg, 2)));
return 0;
}