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).
31 lines
799 B
Plaintext
31 lines
799 B
Plaintext
// impl Protocol for built-in scalar types (f32, i64, bool, u32, ...) —
|
|
// both static dispatch (`f32.lerp(...)`) and protocol-boxed dispatch via
|
|
// `#inline` erasure.
|
|
|
|
Lerpable :: protocol #inline {
|
|
lerp :: (self: *Self, b: Self, t: f32) -> Self;
|
|
}
|
|
|
|
impl Lerpable for f32 {
|
|
lerp :: (self: f32, b: f32, t: f32) -> f32 { self + (b - self) * t }
|
|
}
|
|
|
|
do_lerp :: (a: Lerpable, b: f32, t: f32) -> f32 {
|
|
a.lerp(b, t)
|
|
}
|
|
|
|
main :: () -> void {
|
|
// Static call through impl
|
|
result := f32.lerp(0.0, 10.0, 0.5);
|
|
print("lerp(0, 10, 0.5) = {}\n", result);
|
|
|
|
// Protocol dispatch through #inline erasure
|
|
val : f32 = 0.0;
|
|
p : *f32 = @val;
|
|
l : Lerpable = xx p;
|
|
result2 := do_lerp(l, 10.0, 0.25);
|
|
print("lerp(0, 10, 0.25) = {}\n", result2);
|
|
}
|
|
|
|
#import "modules/std.sx";
|