diff --git a/examples/0619-comptime-metatype-type-info.sx b/examples/0619-comptime-metatype-type-info.sx new file mode 100644 index 00000000..059b488c --- /dev/null +++ b/examples/0619-comptime-metatype-type-info.sx @@ -0,0 +1,34 @@ +// Comptime reflection — `type_info($T)`: reflect a SOURCE enum INTO a `TypeInfo` +// value, then feed that value straight back to `define` to mint a byte-identical +// copy. This is the inverse of `define`'s decode: `type_info` reads a type's +// variants (name + payload type) out of the type table and constructs the same +// `.enum(EnumInfo{ variants })` value the `define` examples write by hand. +// +// Round-trip: `ShapeCopy` is reconstructed purely from `type_info(Shape)` — no +// literal variant list — and constructs/matches like the original. +#import "modules/std.sx"; +#import "modules/std/meta.sx"; + +Shape :: enum { + circle: f64; + rect: i64; + empty; +} + +// Reflect Shape → TypeInfo, then reconstruct an identical nominal enum. +ShapeCopy :: define(declare("ShapeCopy"), type_info(Shape)); + +describe :: (s: ShapeCopy) { + if s == { + case .circle: (r) { print("circle r={}\n", r); } + case .rect: (n) { print("rect n={}\n", n); } + case .empty: { print("empty\n"); } + } +} + +main :: () -> i32 { + describe(.circle(2.5)); + describe(.rect(7)); + describe(.empty); + return 0; +} diff --git a/examples/expected/0619-comptime-metatype-type-info.exit b/examples/expected/0619-comptime-metatype-type-info.exit new file mode 100644 index 00000000..d00491fd --- /dev/null +++ b/examples/expected/0619-comptime-metatype-type-info.exit @@ -0,0 +1 @@ +1 diff --git a/examples/expected/0619-comptime-metatype-type-info.stderr b/examples/expected/0619-comptime-metatype-type-info.stderr new file mode 100644 index 00000000..7819cb46 --- /dev/null +++ b/examples/expected/0619-comptime-metatype-type-info.stderr @@ -0,0 +1,17 @@ +error: type_info is not yet implemented + --> examples/0619-comptime-metatype-type-info.sx:19:43 + | +19 | ShapeCopy :: define(declare("ShapeCopy"), type_info(Shape)); + | ^^^^^^^^^ + +error: cannot infer enum type for '.circle' — use an explicit type or assign to a typed variable + --> examples/0619-comptime-metatype-type-info.sx:30:14 + | +30 | describe(.circle(2.5)); + | ^^^^^^^ + +error: cannot infer enum type for '.rect' — use an explicit type or assign to a typed variable + --> examples/0619-comptime-metatype-type-info.sx:31:14 + | +31 | describe(.rect(7)); + | ^^^^^ diff --git a/examples/expected/0619-comptime-metatype-type-info.stdout b/examples/expected/0619-comptime-metatype-type-info.stdout new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/examples/expected/0619-comptime-metatype-type-info.stdout @@ -0,0 +1 @@ +