Files
sx/examples/0517-packs-pack-reflection-intrinsics.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

63 lines
2.1 KiB
Plaintext

// Variadic heterogeneous type packs — step 3: type-reflection
// intrinsics.
//
// Three comptime helpers used by pack-fn bodies to branch on
// type identity / protocol membership:
//
// type_name(T) -> string // display name of T
// type_eq(T1, T2) -> bool // structural TypeId equality
// has_impl(P, T) -> bool // T has a reachable impl for P
//
// All three fold to compile-time constants and are accepted by
// `tryConstBoolCondition`, so `inline if type_eq(...)` /
// `inline if has_impl(...)` collapse to a single branch at lower
// time — no runtime cost.
//
// `has_impl`'s protocol arg accepts both shapes:
// - plain protocol name: `has_impl(Allocator, CAllocator)`.
// - parameterised call: `has_impl(Wrap(s64), s32)` — the args
// match the impl's protocol type-args exactly.
#import "modules/std.sx";
#import "modules/allocators.sx";
// User-defined parameterised protocol + an impl, so has_impl can
// confirm parameterised matching works with a known-true case.
Wrap :: protocol(Target: Type) {
wrap :: () -> Target;
}
impl Wrap(s64) for s32 {
wrap :: (self: s32) -> s64 => xx self;
}
main :: () -> s32 {
// type_name — display names.
print("{} {} {}\n", type_name(s64), type_name(string), type_name(bool));
// type_eq — structural equality on TypeIds.
print("{} {} {} {}\n",
type_eq(s64, s64),
type_eq(s64, string),
type_eq(*s64, *s64),
type_eq(*s64, *s32));
// inline-if folds type_eq at lower time.
inline if type_eq(s64, s64) {
print("inline-if folded: same\n");
} else {
print("inline-if folded: different\n");
}
// has_impl — plain protocol (Allocator is unary).
print("Allocator/CAllocator: {}\n", has_impl(Allocator, CAllocator));
print("Allocator/s64: {}\n", has_impl(Allocator, s64));
// has_impl — parameterised protocol (Wrap takes a Target type arg).
print("Wrap(s64)/s32: {}\n", has_impl(Wrap(s64), s32));
print("Wrap(s64)/bool: {}\n", has_impl(Wrap(s64), bool));
print("Wrap(bool)/s32: {}\n", has_impl(Wrap(bool), s32));
return 0;
}