A comptime value param whose constraint is a plain enum ($o: Ord) now binds its enum-literal argument to the variant tag during inlined comptime-call lowering. The tag is recorded in comptime_value_bindings (readable downstream via comptimeIntNamed / direct map lookup, and as an array-dim style const-int leaf) AND the param is bound into scope as an enum_init value so body comparisons like 'if o == .a' lower as ordinary enum comparisons. Distinct ordering args monomorphize the inlined body per value. A non-constant argument or an unknown variant emits a loud diagnostic and binds nothing — never a silent default. Locked by examples/0627-comptime-enum-value-param.sx.
28 lines
978 B
Plaintext
28 lines
978 B
Plaintext
// Comptime ENUM value parameter: `$o: <EnumType>` binds the enum-literal
|
|
// argument to its variant tag, monomorphizes the inlined body per distinct
|
|
// ordering value, and resolves `o` in the body as a compile-time-known enum
|
|
// constant — usable both in `if o == .a` comparisons AND as a comptime-readable
|
|
// variant tag during lowering (a downstream lowerer reads it via
|
|
// `comptime_value_bindings`, exercised here as the `[o]i64` array dimension).
|
|
#import "modules/std.sx";
|
|
|
|
Ord :: enum { a; b; c; }
|
|
|
|
pick :: ($o: Ord) -> i64 {
|
|
if o == .a { return 10; }
|
|
if o == .b { return 20; }
|
|
return 30;
|
|
}
|
|
|
|
// `o` read as a compile-time integer (its variant tag) in a TYPE position.
|
|
tag_dim :: ($o: Ord) -> i64 {
|
|
arr : [o]i64 = ---;
|
|
return arr.len;
|
|
}
|
|
|
|
main :: () {
|
|
print("{}\n", pick(.b)); // 20
|
|
print("{} {} {}\n", pick(.a), pick(.b), pick(.c)); // 10 20 30
|
|
print("{} {} {}\n", tag_dim(.a), tag_dim(.b), tag_dim(.c)); // 0 1 2
|
|
}
|