Files
sx/examples/comptime/0620-comptime-metatype-make-enum.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

42 lines
1.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// make_enum — the GENERAL comptime enum constructor over declare/define: mint a
// nominal enum from a `[]EnumVariant` VALUE, rather than a hardcoded literal
// (the channel-result constructors hardcode theirs). The variant list is an
// ordinary comptime value, so a builder ASSEMBLES it in a local before minting —
// here `build_level` constructs `vs` (a local array, which could be filled
// conditionally / in a loop), then mints `Level` from it.
//
// This exercises `define` decoding a value-arg SLICE (`decodeVariantElements`'s
// slice-fat-pointer branch), as opposed to the inline `.[ … ]` array the
// 06140618 examples pass directly into `.enum(...)`.
#import "modules/std.sx";
#import "modules/std/meta.sx";
build_level :: () -> Type {
// The variant list lives in a local (not inlined into `define`): a non-
// generic `() -> Type` builder has its whole body comptime-evaluated, so
// `vs` is in scope when `make_enum` mints from it.
vs := EnumVariant.[
EnumVariant.{ name = "info", payload = void },
EnumVariant.{ name = "warn", payload = void },
EnumVariant.{ name = "fatal", payload = i64 }, // carries an exit code
];
return make_enum("Level", vs);
}
Level :: build_level();
show :: (l: Level) {
if l == {
case .info: { print("info\n"); }
case .warn: { print("warn\n"); }
case .fatal: (c) { print("fatal code={}\n", c); }
}
}
main :: () -> i32 {
show(.info);
show(.warn);
show(.fatal(70));
return 0;
}