diff --git a/examples/0622-comptime-metatype-struct.sx b/examples/0622-comptime-metatype-struct.sx new file mode 100644 index 00000000..86e3338d --- /dev/null +++ b/examples/0622-comptime-metatype-struct.sx @@ -0,0 +1,27 @@ +// Comptime STRUCT metaprogramming — `define` constructs a struct (not just an +// enum), and `type_info` reflects one. Two paths: +// 1. Programmatic build: `define(declare("Vec2"), .struct(.{ fields = … }))` +// mints a nominal struct from a `[]StructField` value. +// 2. Round-trip: `define(declare("RowCopy"), type_info(Row))` reflects a source +// struct INTO a `.struct(StructInfo)` value and reconstructs an identical +// one — no literal field list. +// Both constructed structs build + read like any hand-written struct. +#import "modules/std.sx"; +#import "modules/std/meta.sx"; + +Vec2 :: define(declare("Vec2"), .struct(.{ fields = .[ + StructField.{ name = "x", type = f64 }, + StructField.{ name = "y", type = f64 }, +] })); + +Row :: struct { id: i64; score: f64; } +RowCopy :: define(declare("RowCopy"), type_info(Row)); + +main :: () -> i32 { + v := Vec2.{ x = 1.5, y = -2.0 }; + print("v = {} {}\n", v.x, v.y); + + r := RowCopy.{ id = 42, score = 9.5 }; + print("r = {} {}\n", r.id, r.score); + return 0; +} diff --git a/examples/expected/0622-comptime-metatype-struct.exit b/examples/expected/0622-comptime-metatype-struct.exit new file mode 100644 index 00000000..573541ac --- /dev/null +++ b/examples/expected/0622-comptime-metatype-struct.exit @@ -0,0 +1 @@ +0 diff --git a/examples/expected/0622-comptime-metatype-struct.stderr b/examples/expected/0622-comptime-metatype-struct.stderr new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/examples/expected/0622-comptime-metatype-struct.stderr @@ -0,0 +1 @@ + diff --git a/examples/expected/0622-comptime-metatype-struct.stdout b/examples/expected/0622-comptime-metatype-struct.stdout new file mode 100644 index 00000000..500ba094 --- /dev/null +++ b/examples/expected/0622-comptime-metatype-struct.stdout @@ -0,0 +1,2 @@ +v = 1.500000 -2.000000 +r = 42 9.500000