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).
48 lines
1.5 KiB
Plaintext
48 lines
1.5 KiB
Plaintext
// Protocol methods declare their receiver EXPLICITLY as the first parameter —
|
|
// `self: *Self` (or `self: Self`) — matching the `impl` method signature. This
|
|
// is required (the old implicit-receiver form is a parse error). The receiver
|
|
// annotation is validated then erased, so dispatch and call sites are unchanged:
|
|
// a method with N declared extra args is still called with N args.
|
|
#import "modules/std.sx";
|
|
|
|
Size :: struct { w: i32; h: i32; }
|
|
|
|
// `self: *Self` receiver; one no-arg method and one with extra args.
|
|
Measurable :: protocol #inline {
|
|
measure :: (self: *Self) -> Size;
|
|
scaled :: (self: *Self, factor: i32) -> Size;
|
|
}
|
|
|
|
// `self: Self` (by-value) receiver is also accepted.
|
|
Named :: protocol {
|
|
name :: (self: Self) -> string;
|
|
}
|
|
|
|
Widget :: struct { base: i32; }
|
|
|
|
impl Measurable for Widget {
|
|
measure :: (self: *Widget) -> Size { return Size.{ w = self.base, h = self.base * 2 }; }
|
|
scaled :: (self: *Widget, factor: i32) -> Size {
|
|
return Size.{ w = self.base * factor, h = self.base * 2 * factor };
|
|
}
|
|
}
|
|
|
|
impl Named for Widget {
|
|
name :: (self: Widget) -> string { return "widget"; }
|
|
}
|
|
|
|
main :: () -> i32 {
|
|
w : Widget = .{ base = 5 };
|
|
p : *Widget = @w;
|
|
|
|
m : Measurable = xx p;
|
|
s := m.measure(); // 0 extra args
|
|
s2 := m.scaled(3); // 1 extra arg
|
|
print("measure={}x{}\n", s.w, s.h); // 5x10
|
|
print("scaled={}x{}\n", s2.w, s2.h); // 15x30
|
|
|
|
n : Named = xx p;
|
|
print("name={}\n", n.name()); // widget
|
|
return 0;
|
|
}
|