Break the monolithic examples/50-smoke.sx into 30 focused per-section examples,
filed into their category blocks (basic/types/comptime/memory/protocols/ffi),
each carrying only the top-level decls its section references (the protocols
section keeps the full preamble — its deps flow through UFCS method calls that
name-based extraction can't see). Outputs verified identical to the original
section blocks.
Add examples/1036-errors-failable-smoke.sx — an end-to-end error-handling example
(the E5.4 work): named + inferred error sets consumed via destructure, try (in
helpers), catch (bare-expr / match-body / diverging / no-binding), or
value-terminator, onfail+defer interleave, and error.X value + {} tag
interpolation.
Remove examples/50-smoke.sx. Suite: 324 passed, 0 failed.
105 lines
3.1 KiB
Plaintext
105 lines
3.1 KiB
Plaintext
// End-to-end error handling: failable functions (named + inferred sets) consumed
|
|
// through every absorbing form — destructure, `try` (in helpers), `catch` (bare-
|
|
// expr / match-body / diverging block / no-binding), `or` value-terminator,
|
|
// `onfail` cleanup interleaved with `defer`, plus `error.X` as a value and `{}`
|
|
// tag-name interpolation.
|
|
|
|
#import "modules/std.sx";
|
|
|
|
SmokeErr :: error { Empty, BadDigit, Overflow }
|
|
|
|
// value-carrying, named set
|
|
sm_parse :: (n: s32) -> (s32, !SmokeErr) {
|
|
if n < 0 { raise error.BadDigit; }
|
|
if n == 0 { raise error.Empty; }
|
|
if n > 99 { raise error.Overflow; }
|
|
return n * 2;
|
|
}
|
|
|
|
// pure failable, inferred set (ad-hoc tag)
|
|
sm_check :: (ok: bool) -> ! {
|
|
if !ok { raise error.NotReady; }
|
|
return;
|
|
}
|
|
|
|
// multi-value, inferred set: `try` propagates; SCC absorbs SmokeErr
|
|
sm_pair :: (a: s32, b: s32) -> (s32, s32, !) {
|
|
x := try sm_parse(a);
|
|
y := try sm_parse(b);
|
|
return (x, y);
|
|
}
|
|
|
|
// catch with a diverging block body
|
|
sm_or_default :: (n: s32) -> s32 {
|
|
return sm_parse(n) catch e {
|
|
print(" logged {}\n", e);
|
|
return -1;
|
|
};
|
|
}
|
|
|
|
// onfail + defer interleave: cleanup runs only on the error path
|
|
sm_acquire :: (fail: bool) -> (s32, !) {
|
|
defer print(" defer A\n");
|
|
onfail print(" onfail B\n");
|
|
if fail { raise error.Acquire; }
|
|
return 7;
|
|
}
|
|
|
|
// or-chain: try a, fall to try b; propagate if both fail
|
|
sm_first :: (a: s32, b: s32) -> (s32, !) {
|
|
v := try sm_parse(a) or try sm_parse(b);
|
|
return v;
|
|
}
|
|
|
|
main :: () {
|
|
// error.X as a typed value + {} interpolation renders the tag name
|
|
e0 : SmokeErr = error.BadDigit;
|
|
print("tag: {}\n", e0);
|
|
|
|
// success destructure + error inspect
|
|
v1, err1 := sm_parse(5);
|
|
if !err1 { print("parsed: {}\n", v1); }
|
|
v2, err2 := sm_parse(-1);
|
|
if err2 == error.BadDigit { print("got: {}\n", err2); }
|
|
|
|
// catch — bare-expr body
|
|
ce := sm_parse(0) catch e 100;
|
|
print("catch-expr: {}\n", ce);
|
|
|
|
// catch — match-body per-tag dispatch
|
|
cm := sm_parse(200) catch e == {
|
|
case .Overflow: 1;
|
|
case .Empty: 2;
|
|
else: 3;
|
|
};
|
|
print("catch-match: {}\n", cm);
|
|
|
|
// catch — diverging block (in helper)
|
|
print("or-default ok: {}\n", sm_or_default(5));
|
|
print("or-default err: {}\n", sm_or_default(-5));
|
|
|
|
// or — value terminator (bare LHS; non-failable result)
|
|
ov := sm_parse(0) or 55;
|
|
print("or-value: {}\n", ov);
|
|
|
|
// or-chain via helper (first ok wins; propagation absorbed by destructure)
|
|
g, gerr := sm_first(0, 8);
|
|
if !gerr { print("or-chain: {}\n", g); }
|
|
|
|
// multi-value failable consumed by catch (tuple body)
|
|
p, q := sm_pair(0, 3) catch e (0, 0);
|
|
print("pair-catch: {} {}\n", p, q);
|
|
p2, q2 := sm_pair(4, 5) catch e (0, 0);
|
|
print("pair-ok: {} {}\n", p2, q2);
|
|
|
|
// pure failable: absorb with no-binding catch
|
|
sm_check(false) catch { print("check absorbed\n"); };
|
|
|
|
// onfail/defer interleave on error vs success
|
|
print("acquire fail:\n");
|
|
hv, herr := sm_acquire(true);
|
|
print("acquire ok:\n");
|
|
iv, ierr := sm_acquire(false);
|
|
print("errors ok\n");
|
|
}
|