Files
sx/examples/0526-packs-protocol-pack.sx
agra 4e942b5373 test: migrate examples to XXXX-category-name layout + split expected streams
Rename all example tests/companions to the XXXX-category-test-name scheme
(per-category 100-blocks: basic 0010, types 0100, ... errors 1000,
diagnostics 1100, ffi 1200, ffi-objc 1300, ffi-jni 1400, vectors 1500,
platform 1600). Companions and dir/C fixtures move in lockstep with their
parent test; #import/#source/#include paths rewritten to match.

Expected output now lives in examples/expected/ (a sibling dir of the
tests) split into three streams per the new convention:
  <name>.exit / <name>.stdout / <name>.stderr  (+ optional <name>.ir)

run_examples.sh rewritten: scans examples/ and issues/ for an
expected/<name>.exit marker, captures stdout and stderr separately (no
more 2>&1), compares each stream + exit + optional IR snapshot.

Behavior validated unchanged: every renamed test reproduces its prior
merged output + exit (diffs limited to file paths/basenames embedded in
diagnostics + traces, which correctly reflect the new names). Suite:
292 passed, 0 failed. 50-smoke.sx split + issue relocation + docs follow
in subsequent commits.
2026-06-01 19:05:15 +03:00

40 lines
1.5 KiB
Plaintext

// Feature 1 — heterogeneous protocol-constrained variadic pack (binding).
//
// `..xs: Show` (no `[]`, no `$`) is a pack, not a slice: each call site
// monomorphizes with the concrete per-position arg types (Decision 1 — a pack
// is a comptime mechanism, no runtime pack value), and `xs.len` is a comptime
// constant. Arity and element types vary per call; each call is a distinct
// specialization.
//
// DESIGN (locked): a pack element is viewed THROUGH the protocol — only the
// protocol's own interface (its methods, and the projections `xs.T` / `xs.value`)
// is accessible, NOT arbitrary concrete members. So `xs[i].get()` / `xs.T` are
// the intended interface; `xs[i].v` (a field on the concrete IntBox, not part
// of `Show`) is an error. Those are still being implemented — this example
// locks in only the binding + comptime `xs.len`, the protocol-agnostic part.
#import "modules/std.sx";
Show :: protocol(T: Type) {
get :: (self: *Self) -> T;
}
IntBox :: struct { v: s64; }
StrBox :: struct { s: string; }
impl Show(s64) for IntBox { get :: (self: *IntBox) -> s64 => self.v; }
impl Show(string) for StrBox { get :: (self: *StrBox) -> string => self.s; }
howmany :: (..xs: Show) -> s64 {
return xs.len;
}
main :: () -> s32 {
a := IntBox.{ v = 42 };
b := IntBox.{ v = 7 };
c := StrBox.{ s = "cool" };
print("n0={}\n", howmany()); // empty pack
print("n2={}\n", howmany(a, b)); // two elements
print("n3={}\n", howmany(a, b, c)); // heterogeneous: IntBox, IntBox, StrBox
0;
}