feat(metatype): make_enum — general enum constructor over a []EnumVariant value

make_enum(name, variants: []EnumVariant) -> Type mints a nominal enum
from a variant list passed as a VALUE, not a hardcoded literal — the
open-ended form the channel-result constructors are special cases of.
Pure sx over declare/define; no compiler machinery.

Because variants is an ordinary comptime value, a non-generic builder
can ASSEMBLE it in a local before minting. examples/0620: build_level
fills a local array, then make_enum mints Level from it — exercising
define decoding a value-arg SLICE (decodeVariantElements' slice branch),
vs. the inline .[ … ] array the 0614-0618 examples pass directly.

No compiler change (locks existing capability). Suite green (678).
This commit is contained in:
agra
2026-06-17 04:55:48 +03:00
parent 0cb1aa270e
commit 2250652ba5
5 changed files with 63 additions and 0 deletions

View File

@@ -57,6 +57,23 @@ field_type :: ($T: Type, idx: i64) -> Type #builtin;
// and is one nominal type across sites (the type-fn identity path). The channel
// library (N3) consumes these once it lands.
// The GENERAL enum constructor: mint a nominal enum NAMED `name` from a variant
// list passed as a VALUE (a `[]EnumVariant`), rather than a hardcoded literal.
// Because `variants` is an ordinary comptime value, a caller can ASSEMBLE it in
// a local (conditionally, in a loop, from type args) before minting — see
// `examples/0620`. `define` decodes the slice via `decodeVariantElements`. The
// channel constructors above are the special-cased shapes; `make_enum` is the
// open-ended one every other constructor could be written over.
//
// Call it from a non-generic `() -> Type` builder (whose whole body is
// comptime-evaluated, so locals are in scope) or inline with a literal arg
// (`E :: make_enum("E", .[ … ])`). A *generic* type-fn comptime-evaluates only
// its return EXPRESSION, so build the list inline in the return there, not in a
// preceding local.
make_enum :: (name: string, variants: []EnumVariant) -> Type {
return define(declare(name), .enum(.{ variants = variants }));
}
// A blocking recv: a value, or the channel was closed (drained).
RecvResult :: ($T: Type) -> Type {
return define(declare("RecvResult"), .enum(.{ variants = .[