test(metatype): generic type-fn body local (examples/0624)
Locks the generic-type-fn prelude eval (d87d86d): make_status($T)
assembles a variant list in a local then mints, with the ok payload = T.
This commit is contained in:
34
examples/0624-comptime-metatype-generic-typefn-local.sx
Normal file
34
examples/0624-comptime-metatype-generic-typefn-local.sx
Normal file
@@ -0,0 +1,34 @@
|
||||
// A GENERIC type-fn (`($T) -> Type`) may use LOCALS before its `return` — the
|
||||
// full body is comptime-evaluated, not just the return expression. Here
|
||||
// `make_status` assembles a variant list in a local `vs` (whose `ok` payload is
|
||||
// the type parameter `T`), then mints `Status` from it via `make_enum`. The
|
||||
// equivalent in a non-generic builder already worked (examples/0620); this
|
||||
// extends it to the generic case.
|
||||
#import "modules/std.sx";
|
||||
#import "modules/std/meta.sx";
|
||||
|
||||
make_status :: ($T: Type) -> Type {
|
||||
vs := EnumVariant.[
|
||||
EnumVariant.{ name = "ok", payload = T }, // payload = the type arg
|
||||
EnumVariant.{ name = "pending", payload = void },
|
||||
EnumVariant.{ name = "failed", payload = i64 }, // error code
|
||||
];
|
||||
return make_enum("Status", vs);
|
||||
}
|
||||
|
||||
Status :: make_status(f64);
|
||||
|
||||
show :: (s: Status) {
|
||||
if s == {
|
||||
case .ok: (v) { print("ok={}\n", v); }
|
||||
case .pending: { print("pending\n"); }
|
||||
case .failed: (e) { print("failed code={}\n", e); }
|
||||
}
|
||||
}
|
||||
|
||||
main :: () -> i32 {
|
||||
show(.ok(2.5));
|
||||
show(.pending);
|
||||
show(.failed(404));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
ok=2.500000
|
||||
pending
|
||||
failed code=404
|
||||
Reference in New Issue
Block a user