Files
sx/examples/1044-errors-generic-failable-composition.sx
agra 1c14383495 ERR/E5.1: verify generic failable composition (sub-feature 8); resolve 0062, file 0064
Generic value-carrying failable composition works with the documented
$T: Type generic form (catch / destructure / failure-propagation / a
second monomorphization at a different T). Issue 0062 was an invalid-repro
report — it used the non-generic T: type form, which is a plain Type-valued
param, not a generic type parameter. Marked 0062 resolved (not a bug).

The only real residual: a non-$ T: Type function param used as a type
silently resolves to an empty {} (renders T{}) instead of erroring. Filed
as 0064 (deferred, orthogonal to ERR — the $T idiom works).

Regression: 1044-errors-generic-failable-composition.sx.
2026-06-01 22:35:02 +03:00

30 lines
1.3 KiB
Plaintext

// Generic function with a value-carrying `!` return composes (ERR E5.1
// sub-feature 8). A `$T: Type` generic whose return is `(T, !E)` monomorphizes
// per call: `return try f()` propagates the closure's error, and each
// monomorphization's success value flows through as the concrete `T`.
// (Regression: confirms issue 0062 was an invalid-syntax repro — the bug only
// appeared with the non-generic `T: Type` form; the `$T` form works.)
#import "modules/std.sx";
E :: error { Bad }
wrap :: ($T: Type, f: Closure() -> (T, !E)) -> (T, !E) { return try f(); }
main :: () -> s32 {
// success, consumed by catch
print("catch={}\n", wrap(s32, closure(() -> (s32, !E) { return 7; })) catch e -1); // 7
// success, consumed by destructure (binds value + error slot)
r, err := wrap(s32, closure(() -> (s32, !E) { return 9; }));
no_err := if err == error.Bad then false else true;
print("destr={} ok={}\n", r, no_err); // destr=9 ok=true
// failure path: the raised tag propagates through the generic `try`
print("fail={}\n", wrap(s32, closure(() -> (s32, !E) { raise error.Bad; }) ) catch e -1); // -1
// a second monomorphization at a different T
print("u8={}\n", wrap(u8, closure(() -> (u8, !E) { return 200; })) catch e 0); // 200
return 0;
}