Files
sx/examples/0903-optionals-optional-roundtrip.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

57 lines
1.7 KiB
Plaintext

// Optional `?f32` fields in struct literals — exhaustively combine null/value
// for both fields, through both direct calls and protocol dispatch.
#import "modules/std.sx";
ProposedSize :: struct {
width: ?f32;
height: ?f32;
}
// Direct function — does it work?
direct_size :: (proposal: ProposedSize) -> f32 {
w := if pw := proposal.width { pw } else { 100.0 };
h := if ph := proposal.height { ph } else { 100.0 };
w + h
}
Sizable :: protocol {
size :: (self: *Self, proposal: ProposedSize) -> f32;
}
Widget :: struct {}
impl Sizable for Widget {
size :: (self: *Widget, proposal: ProposedSize) -> f32 {
w := if pw := proposal.width { pw } else { 100.0 };
h := if ph := proposal.height { ph } else { 100.0 };
w + h
}
}
main :: () -> void {
// Test 1: Direct call
print("=== Direct calls ===\n");
d1 := direct_size(ProposedSize.{ width = 50.0, height = null });
print("d1 = {}\n", d1);
d2 := direct_size(ProposedSize.{ width = null, height = 50.0 });
print("d2 = {}\n", d2);
d3 := direct_size(ProposedSize.{ width = null, height = null });
print("d3 = {}\n", d3);
d4 := direct_size(ProposedSize.{ width = 50.0, height = 60.0 });
print("d4 = {}\n", d4);
// Test 2: Protocol dispatch
print("=== Protocol dispatch ===\n");
w := Widget.{};
s : Sizable = w;
r1 := s.size(ProposedSize.{ width = 50.0, height = null });
print("r1 = {}\n", r1);
r2 := s.size(ProposedSize.{ width = null, height = 50.0 });
print("r2 = {}\n", r2);
r3 := s.size(ProposedSize.{ width = null, height = null });
print("r3 = {}\n", r3);
r4 := s.size(ProposedSize.{ width = 50.0, height = 60.0 });
print("r4 = {}\n", r4);
}