Files
sx/library/modules/std/meta.sx
agra 5f2419854e green: erase the sx reify sugar — declare/define are the only constructors
Per the directive to strip reify entirely: the sx `reify(info)` one-shot is
removed. `define(handle, info)` now RETURNS the (completed) handle, so the
one-shot constructor chains as a single expression:
    T :: define(declare(), .enum(.{ name = "T", variants = ... }));

- meta.sx: drop reify; RecvResult/TryResult use `define(declare(), …)`.
- interp .define returns the handle type_tag (was void); call.zig lowers it
  with `Type` result and sets the info arg's target type to TypeInfo so the
  intercepted call still infers the `.enum(…)` literal.
- returnExprMintsType: a type-fn body that returns `define(…)` (or a bodied
  non-generic Type-returning sx helper) is comptime-evaluated.
- examples 0614 (direct) + 0615 (type-fn) use `define(declare(), …)`.

Full suite green (673). Files/docs still carry the old reify naming — the
rename sweep is the next commit.
2026-06-16 21:12:32 +03:00

79 lines
3.6 KiB
Plaintext

// Comptime type metaprogramming — `declare` / `define` (construct a NEW nominal
// type from data), plus `type_info` / `field_type` (reflect a type → data) and
// the data model they reflect INTO and construct FROM.
//
// This is a SEPARATE on-demand module rather than part of the prelude: its data
// types would otherwise intern into every module's type table and shift every
// `.ir` snapshot. Import it explicitly: #import "modules/std/meta.sx";
//
// All four are comptime-only builtins — reaching one at runtime is a hard error
// (the type must be minted / reflected at compile time).
// One variant of a constructed enum: a name plus an optional payload type.
// `payload = void` means a tagless variant (e.g. `closed`).
EnumVariant :: struct {
name: string;
payload: Type;
}
// The shape of an enum/tagged-union being reflected or constructed. `name` is
// the type's name — it travels WITH the shape (so `define` can name the slot and
// `type_info` round-trips it); the compiler derives nothing from a binding LHS.
EnumInfo :: struct {
name: string;
variants: []EnumVariant;
}
// The reflected/constructed type shape. A tagged union over the kinds of type
// that can be minted. Only `` .`enum `` ships today; struct/tuple land later.
// The variant uses the backtick raw-identifier escape so it reads as the
// keyword `enum` (`` .`enum(...) ``) rather than a mangled `enum_`.
TypeInfo :: enum {
`enum: EnumInfo;
}
// The compiler's ONLY type-construction primitives (comptime-only #builtins):
// declare() — mint a NEW empty (undefined) nominal type, returned
// as a `Type` handle. Using it before its `define` is a
// loud error; references to it (`*Self`) are fine.
// define(handle, info) — fill a declared handle's body from a `TypeInfo`
// (which carries the type's name), and RETURN the
// handle so the one-shot form chains:
// T :: define(declare(), info);
// The recursive / mutually-recursive form keeps them apart so the handle can be
// referenced inside its own definition:
// List :: declare();
// define(List, .enum(.{ name = "List", variants = .[
// EnumVariant.{ name = "cons", payload = *List },
// EnumVariant.{ name = "nil", payload = void } ] }));
declare :: () -> Type #builtin;
define :: (handle: Type, info: TypeInfo) -> Type #builtin;
type_info :: ($T: Type) -> TypeInfo #builtin;
field_type :: ($T: Type, idx: i64) -> Type #builtin;
// --- Type constructors built in sx library code (no compiler machinery) ---
//
// The channel result types, expressed as type-fns over declare/define. They
// demonstrate that a programmatically-built enum carries a full enum through
// codegen: `RecvResult(i64)` constructs and matches like any hand-written enum,
// and is one nominal type across sites (the type-fn identity path). The channel
// library (N3) consumes these once it lands.
// A blocking recv: a value, or the channel was closed (drained).
RecvResult :: ($T: Type) -> Type {
return define(declare(), .enum(.{ name = "RecvResult", variants = .[
EnumVariant.{ name = "value", payload = T },
EnumVariant.{ name = "closed", payload = void },
] }));
}
// A non-blocking try-recv: a value, currently empty, or closed — three states
// a bool can't express.
TryResult :: ($T: Type) -> Type {
return define(declare(), .enum(.{ name = "TryResult", variants = .[
EnumVariant.{ name = "value", payload = T },
EnumVariant.{ name = "empty", payload = void },
EnumVariant.{ name = "closed", payload = void },
] }));
}