Files
sx/examples/98-optional-protocol.sx
agra 79419b99bd issue-0028: ?Protocol = null sentinel-shaped optional protocols
Protocol structs registered via registerProtocolDecl carry a new
is_protocol flag; the ?T paths in sizeOf/typeSizeBytes/toLLVMType
recognise it and lay out ?Protocol as the protocol struct itself
(ctx == null IS the "none" state), matching how ?Closure / ?*T are
sentinel-shaped — no extra storage.

Method dispatch on ?Protocol auto-unwraps in lowerCall's field-access
path; the unwrap is structurally a no-op so we just rebind obj_ty to
the payload type. resolveCallParamTypes extended for optional-protocol
receivers so enum-literal args (gpu.create_texture(.r8, ...)) get the
right target_type and don't silently collapse to tag=0 : s32 — same
issue-0031-class bug closed in Session 66, one type-system layer
deeper.

Library: UIRenderer / UIPipeline / GlyphCache migrated from the verbose
gpu: GPU = ---; has_gpu: bool pattern to gpu: ?GPU = null. set_gpu no
longer maintains a parallel bool flag.

Bundled: dock.sx threads delta_time as a struct field rather than via
a global pointer (cleanup unrelated to issue-0028, committed alongside).

Verified: 85/85 regression tests pass; iOS-sim chess + macOS chess
both render correctly post-migration.
2026-05-18 18:32:55 +03:00

35 lines
778 B
Plaintext

// `?Protocol = null` — optional protocol boxes use sentinel-shape
// (ctx == null is the "none" state), so they cost no extra storage
// beyond the protocol's standard 2-pointer layout. Method calls on
// a non-null optional protocol auto-unwrap and dispatch through the
// vtable / inline fn-ptrs as usual.
#import "modules/std.sx";
GPU :: protocol {
ping :: () -> s64;
}
Impl :: struct {}
impl GPU for Impl {
ping :: (self: *Impl) -> s64 { 42; }
}
main :: () -> s32 {
g : ?GPU = null;
if g != null {
print("BAD: g not null at start\n");
} else {
print("g initially null\n");
}
g = xx @Impl.{};
if g != null {
n := g.ping();
print("after assign: g.ping() = {}\n", n);
} else {
print("BAD: g still null after assign\n");
}
0;
}