Files
sx/examples/comptime/0616-comptime-field-type.sx
agra 66bdc70bf1 test: group examples into per-category folders
Move examples/*.sx and their expected/ snapshots into per-category
subfolders (examples/<category>/...). Folder = leading filename token,
with ffi-objc/ffi-jni kept whole; filenames are unchanged. The corpus
runner and LSP sweep now discover each category's expected/ dir, while
issues/ stays flat. Example 1058's repo-root-relative companion import
is made file-relative. Path strings embedded in 164 snapshots were
regenerated (path-only changes). Test-layout docs in CLAUDE.md updated.
2026-06-21 14:41:34 +03:00

31 lines
1.4 KiB
Plaintext

// Comptime reflection: `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;
}