`..xs: Protocol` now binds like the comptime `..$args` pack instead of falling through to a runtime `[]Protocol` slice: each call site monomorphizes with the concrete per-position arg types, and `xs[i]` is the concrete element via AST substitution (Decision 1 — a pack is a comptime mechanism, no runtime pack value). So `xs[i]`'s own fields/methods dispatch statically and elements may be heterogeneous, while `xs.len` is a comptime constant. Mechanism: one `isPackParam(p) = is_variadic and (is_comptime or is_pack)` predicate replaces the four `is_variadic and is_comptime` pack-detection sites (call-arg split, mangle, arg lowering, monomorphizePackFn), and the early call dispatch routes any `isPackFn` call to `lowerPackFnCall` before the `hasComptimeParams` gate (which is false for a protocol pack). examples/191-protocol-pack.sx exercises N=0, N=2, concrete field access, and a heterogeneous IntBox+StrBox pack. Conformance checking and projection (`xs.T` / `xs.value`) are the remaining 2.4 work.
47 lines
1.5 KiB
Plaintext
47 lines
1.5 KiB
Plaintext
// Feature 1 — heterogeneous protocol-constrained variadic pack.
|
|
//
|
|
// `..xs: Show` (no `[]`, no `$`) is a pack, not a slice: each call site
|
|
// monomorphizes with the concrete per-position arg types, and `xs[i]` IS the
|
|
// concrete element (comptime substitution, Decision 1) — so its own fields /
|
|
// methods dispatch statically. `xs.len` is a comptime constant. Elements may
|
|
// be DIFFERENT concrete types as long as each conforms to the protocol.
|
|
//
|
|
// (Conformance checking and projection `xs.T` / `xs.value` are still to come;
|
|
// this locks in the per-shape binding + concrete-element access.)
|
|
|
|
#import "modules/std.sx";
|
|
|
|
Show :: protocol(T: Type) {
|
|
get :: (self: *Self) -> T;
|
|
}
|
|
|
|
IntBox :: struct { v: s64; }
|
|
StrBox :: struct { s: string; }
|
|
impl Show(s64) for IntBox { get :: (self: *IntBox) -> s64 => self.v; }
|
|
impl Show(string) for StrBox { get :: (self: *StrBox) -> string => self.s; }
|
|
|
|
howmany :: (..xs: Show) -> s64 {
|
|
return xs.len;
|
|
}
|
|
|
|
sum2 :: (..xs: Show) -> s64 {
|
|
return xs[0].v + xs[1].v;
|
|
}
|
|
|
|
describe :: (..xs: Show) -> void {
|
|
// Heterogeneous: xs[0] is an IntBox, xs[1] a StrBox.
|
|
print("describe len={} first={} second={}\n", xs.len, xs[0].v, xs[1].s);
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
a := IntBox.{ v = 42 };
|
|
b := IntBox.{ v = 7 };
|
|
print("count0={}\n", howmany()); // N=0 pack
|
|
print("count2={}\n", howmany(a, b)); // N=2
|
|
print("sum={}\n", sum2(a, b)); // concrete field access via pack
|
|
|
|
c := StrBox.{ s = "cool" };
|
|
describe(a, c); // heterogeneous IntBox + StrBox
|
|
0;
|
|
}
|