ERR/E2: multi-value failables -> (T1, ..., !)
Generalize the single-value `-> (T, !)` error-channel ABI to any value arity. Retire the five `fields.len == 2` bails (lowerFailableSuccessReturn, lowerTry, lowerCatch, lowerFailableOr, and the inferExprType try/catch/or arms); lowerRaise + emitErrorReturn already looped over N value slots. New helpers centralize "value-part = every slot but the last (error) one": failableSuccessType (lone value type, or a value-tuple), extractSuccessValue, extractErrorSlot. Fix one latent bug the feature surfaced: coerceToType had no tuple->tuple arm, so a value-tuple flowing into a differently-typed success slot (e.g. (s64,s64) catch body into (s32,s32)) fell through unchanged. Add element-wise coercion. No lowerTupleLiteral change is needed: a `return (a, b)` literal against a 3-field failable target already gets target_fields=null via the arity mismatch, so it types as a plain value-tuple that lowerFailableSuccessReturn consumes. examples/235-multi-value-failable.sx exercises producer return/raise, destructure (binding every slot incl. the error tag), multi-value try (success + propagation), catch (bare-expr tuple body), and or-tuple terminator. Match-body tuple arms are left out: `(` after `case PAT:` is parsed as a payload capture (a pre-existing, multi-value-unrelated parser bug). Gates: zig build, zig build test, 273/273 examples.
This commit is contained in:
64
examples/235-multi-value-failable.sx
Normal file
64
examples/235-multi-value-failable.sx
Normal file
@@ -0,0 +1,64 @@
|
||||
// Multi-value value-carrying failables (ERR — the multi-value error-channel
|
||||
// ABI). A `-> (T1, T2, !E)` function returns EITHER a value-tuple OR an error:
|
||||
// `return (a, b)` yields the success tuple `{a, b, 0}` (the compiler appends the
|
||||
// no-error slot) and `raise error.X` yields `{undef, undef, tag}`. Every consumer
|
||||
// generalizes from the single-value shape: a destructure binds every slot
|
||||
// INCLUDING the error (dropping it is the spec'd discard error — bind it and
|
||||
// inspect); `try` binds the value-tuple on success and propagates `{undef..., tag}`
|
||||
// on failure; `catch` / `or` absorb the failure and merge the value-tuple or the
|
||||
// handler/terminator value. Single-value `-> (T, !E)` is examples/228-231.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
E :: error { Bad, Empty }
|
||||
|
||||
parse :: (n: s32) -> (s32, s32, !E) {
|
||||
if n < 0 { raise error.Bad; }
|
||||
if n == 0 { raise error.Empty; }
|
||||
return (n * 2, n + 1); // success → {n*2, n+1, 0}
|
||||
}
|
||||
|
||||
// Multi-value `try` in a multi-value caller — propagates {undef, undef, tag}.
|
||||
inc :: (n: s32) -> (s32, s32, !E) {
|
||||
v, b := try parse(n);
|
||||
return (v + 1, b + 1);
|
||||
}
|
||||
|
||||
// Multi-value `catch`, bare-expression tuple fallback (absorbs the failure).
|
||||
safe :: (n: s32) -> s32 {
|
||||
v, b := parse(n) catch e (40, 50);
|
||||
return v + b;
|
||||
}
|
||||
|
||||
// Multi-value `or (tuple)` value-terminator (absorbs the failure).
|
||||
ortest :: (n: s32) -> s32 {
|
||||
v, b := parse(n) or (7, 8);
|
||||
return v + b;
|
||||
}
|
||||
|
||||
main :: () -> s32 {
|
||||
r : s32 = 0;
|
||||
|
||||
// Destructure binds EVERY slot including the error tag (e1 / e2 / e3) —
|
||||
// the error is treated, never dropped.
|
||||
v1, b1, e1 := parse(5); // success → (10, 6, no-error)
|
||||
if e1 == error.Bad { r = r + 1000; } // false
|
||||
r = r + v1 + b1; // +16
|
||||
|
||||
v2, b2, e2 := parse(-1); // Bad → {undef, undef, Bad}
|
||||
if e2 == error.Bad { r = r + 4; } // +4
|
||||
|
||||
a, c, ea := inc(5); // parse(5)=(10,6) → (11, 7, no-error)
|
||||
if ea == error.Bad { r = r + 2000; } // false
|
||||
r = r + a + c; // +18
|
||||
|
||||
a2, c2, e3 := inc(-1); // try parse(-1)=Bad → propagate {undef, undef, Bad}
|
||||
if e3 == error.Bad { r = r + 5; } // +5
|
||||
|
||||
r = r + safe(5); // (10, 6) → 16
|
||||
r = r + safe(-1); // Bad → catch → (40, 50) → 90
|
||||
r = r + ortest(0); // Empty → or → (7, 8) → 15
|
||||
|
||||
print("multi-value result: {}\n", r); // 16+4+18+5+16+90+15 = 164
|
||||
return r;
|
||||
}
|
||||
Reference in New Issue
Block a user