Files
sx/examples/0416-protocols-auto-type-erasure.sx
agra 6b0ebdd92b lang: require explicit receiver in protocol method declarations
Protocol method declarations now declare their receiver explicitly as the first
parameter — 'self: *Self' (or 'self: Self') — matching the impl method signature,
instead of the old implicit-receiver form where the listed params were only the
extra args. That asymmetry repeatedly caused confusion over whether the first
param was the receiver or an argument.

The parser validates the first param is 'self' typed Self/*Self, then strips it,
so all downstream lowering and the dispatch ABI are unchanged (impl blocks and
call sites are unaffected). A protocol method missing the receiver is now a parse
error.

Migrated all 129 protocol method signatures across library + examples (+ one
inline-sx test in sema.zig) to the explicit form. Updated specs.md + readme.md.

New: examples/0418-protocols-explicit-receiver.sx (feature),
examples/1190-diagnostics-protocol-missing-receiver.sx (negative/diagnostic).
2026-06-21 11:02:16 +03:00

93 lines
2.3 KiB
Plaintext

#import "modules/std.sx";
#import "modules/math";
#import "modules/build.sx";
#import "modules/std/test.sx";
pkg :: #import "tests/fixtures/testpkg";
Point :: struct { x, y: i32; }
add :: (a: i32, b: i32) -> i32 { a + b }
Counter :: protocol {
inc :: (self: *Self);
get :: (self: *Self) -> i32;
}
Summable :: protocol {
sum :: (self: *Self) -> i32;
}
SimpleCounter :: struct { val: i32; }
impl Counter for SimpleCounter {
inc :: (self: *SimpleCounter) { self.val += 1; }
get :: (self: *SimpleCounter) -> i32 { self.val }
}
impl Summable for Point {
sum :: (self: *Point) -> i32 { self.x + self.y }
}
// Phase 2: #inline protocol for dynamic dispatch
// Phase 2: #inline protocol for dynamic dispatch
Adder :: protocol #inline {
add :: (self: *Self, n: i32);
value :: (self: *Self) -> i32;
}
Accumulator :: struct {
total: i32;
}
impl Adder for Accumulator {
add :: (self: *Accumulator, n: i32) { self.total += n; }
value :: (self: *Accumulator) -> i32 { self.total }
}
main :: () {
// --- Auto type erasure (AE) ---
print("=== Auto Type Erasure ===\n");
// AE1: function argument — concrete passed where protocol expected (no xx)
{
use_counter :: (c: Counter) -> i32 { c.inc(); c.inc(); c.get() }
sc := SimpleCounter.{ val = 10 };
result := use_counter(sc);
print("AE1: {}\n", result);
}
// AE2: variable assignment — concrete to protocol variable (no xx)
{
acc := Accumulator.{ total = 0 };
a: Adder = acc;
a.add(5);
a.add(3);
print("AE2: {}\n", a.value());
}
// AE3: struct literal passed directly (no xx)
{
use_counter :: (c: Counter) -> i32 { c.inc(); c.inc(); c.get() }
result := use_counter(SimpleCounter.{ val = 100 });
print("AE3: {}\n", result);
}
// AE4: explicit xx still works (not broken)
{
use_counter :: (c: Counter) -> i32 { c.inc(); c.get() }
result := use_counter(xx SimpleCounter.{ val = 50 });
print("AE4: {}\n", result);
}
// AE5: pointer auto-erasure — *ConcreteType to protocol
{
use_adder :: (a: Adder) { a.add(10); }
acc := Accumulator.{ total = 5 };
p := @acc;
use_adder(p);
print("AE5: {}\n", acc.total);
}
}