Files
sx/examples/194-protocol-pack-parameterized.sx
agra e604868ffb lang 2.4: parameterized-protocol method calls on pack elements
`xs[i].get()` on a parameterised `..xs: Box(T)` pack now resolves — the
canonical `ValueListenable` shape. registerParamImpl, for a CONCRETE-struct
source, now also registers the impl's methods as `<Source>.<method>` in
fn_ast_map (like a non-parameterised impl), so UFCS finds them. Such methods
are already fully concrete (`impl Box(s64) for IntCell` → `get(self: *IntCell)
-> s64`), so there's nothing to monomorphize; generic/pack sources stay lazy in
param_impl_map. First impl wins on a name collision.

Heterogeneous parameterised packs work: each `xs[i]` binds a different T and
dispatches to its own impl. Regression:
examples/194-protocol-pack-parameterized.sx (Box(s64) IntCell + Box(string)
StrCell, order-independent).
2026-05-29 19:24:06 +03:00

31 lines
1.1 KiB
Plaintext

// Feature 1 — method calls on a PARAMETERIZED protocol pack (the canonical
// shape: `..xs: ValueListenable` where each element conforms with its own
// type-arg). Calling the protocol method `get()` on `xs[i]` resolves to the
// concrete element's impl, even though each element binds a different `T`.
//
// (Parameterised-protocol impl methods with a concrete source type are now
// registered as `<Source>.<method>`, so UFCS — and thus `xs[i].get()` —
// resolves them.)
#import "modules/std.sx";
Box :: protocol(T: Type) {
get :: () -> T;
}
IntCell :: struct { v: s64; }
StrCell :: struct { s: string; }
impl Box(s64) for IntCell { get :: (self: *IntCell) -> s64 => self.v; }
impl Box(string) for StrCell { get :: (self: *StrCell) -> string => self.s; }
describe :: (..xs: Box) -> void {
// xs[0] : Box(s64), xs[1] : Box(string) — different type-args per position.
print("first={} second={}\n", xs[0].get(), xs[1].get());
}
main :: () -> s32 {
describe(IntCell.{ v = 11 }, StrCell.{ s = "hi" });
describe(StrCell.{ s = "x" }, IntCell.{ v = 99 });
0;
}