Files
sx/examples/generics/0215-generics-infer-through-pointer.sx
agra 66bdc70bf1 test: group examples into per-category folders
Move examples/*.sx and their expected/ snapshots into per-category
subfolders (examples/<category>/...). Folder = leading filename token,
with ffi-objc/ffi-jni kept whole; filenames are unchanged. The corpus
runner and LSP sweep now discover each category's expected/ dir, while
issues/ stays flat. Example 1058's repo-root-relative companion import
is made file-relative. Path strings embedded in 164 snapshots were
regenerated (path-only changes). Test-layout docs in CLAUDE.md updated.
2026-06-21 14:41:34 +03:00

30 lines
1.3 KiB
Plaintext

// Generic `$T` inferred through a generic-struct argument head — both
// by-value (`Box($T)`) and pointer-wrapped (`*Box($T)`), the latter also
// via a UFCS dot-call (auto-address-of receiver). Multi-param heads
// (`Pair($A, $B)`) and nested heads (`Box(Box($T))`) bind positionally.
// Regression (issue 0151, widened): `extractTypeParam` had no
// `parameterized_type_expr` arm, so `$T` never bound from a generic-struct
// param — the call failed with "cannot infer generic type parameter 'T'".
#import "modules/std.sx";
Box :: struct ($T: Type) { v: T; }
Pair :: struct ($A: Type, $B: Type) { a: A; b: B; }
unbox :: (b: *Box($T)) -> $T { return b.v; } // infer through `*`
byval :: (b: Box($T)) -> $T { return b.v; } // infer through head
second :: (p: Pair($A, $B)) -> $B { return p.b; } // 2nd of two params
nested :: (b: Box(Box($T))) -> $T { return b.v.v; } // nested head
get :: ufcs (b: *Box($T)) -> $T { return b.v; } // UFCS auto-ref
main :: () -> i32 {
b : Box(i64) = .{ v = 42 };
p : Pair(i64, f64) = .{ a = 1, b = 2.5 };
nb : Box(Box(i64)) = .{ v = .{ v = 9 } };
print("unbox={}\n", unbox(@b));
print("byval={}\n", byval(b));
print("second={}\n", second(p));
print("nested={}\n", nested(nb));
print("ufcs={}\n", b.get());
return 0;
}