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.
93 lines
2.2 KiB
Plaintext
93 lines
2.2 KiB
Plaintext
#import "modules/std.sx";
|
|
#import "modules/math/math.sx";
|
|
#import "modules/compiler.sx";
|
|
#import "modules/test.sx";
|
|
pkg :: #import "modules/testpkg";
|
|
|
|
Point :: struct { x, y: s32; }
|
|
|
|
add :: (a: s32, b: s32) -> s32 { a + b; }
|
|
|
|
Counter :: protocol {
|
|
inc :: ();
|
|
get :: () -> s32;
|
|
}
|
|
|
|
Summable :: protocol {
|
|
sum :: () -> s32;
|
|
}
|
|
|
|
SimpleCounter :: struct { val: s32; }
|
|
|
|
impl Counter for SimpleCounter {
|
|
inc :: (self: *SimpleCounter) { self.val += 1; }
|
|
get :: (self: *SimpleCounter) -> s32 { self.val; }
|
|
}
|
|
|
|
impl Summable for Point {
|
|
sum :: (self: *Point) -> s32 { self.x + self.y; }
|
|
}
|
|
|
|
// Phase 2: #inline protocol for dynamic dispatch
|
|
|
|
// Phase 2: #inline protocol for dynamic dispatch
|
|
Adder :: protocol #inline {
|
|
add :: (n: s32);
|
|
value :: () -> s32;
|
|
}
|
|
|
|
Accumulator :: struct {
|
|
total: s32;
|
|
}
|
|
|
|
impl Adder for Accumulator {
|
|
add :: (self: *Accumulator, n: s32) { self.total += n; }
|
|
value :: (self: *Accumulator) -> s32 { self.total; }
|
|
}
|
|
|
|
main :: () {
|
|
|
|
// --- Auto type erasure (AE) ---
|
|
print("=== Auto Type Erasure ===\n");
|
|
|
|
// AE1: function argument — concrete passed where protocol expected (no xx)
|
|
{
|
|
use_counter :: (c: Counter) -> s32 { c.inc(); c.inc(); c.get(); }
|
|
sc := SimpleCounter.{ val = 10 };
|
|
result := use_counter(sc);
|
|
print("AE1: {}\n", result);
|
|
}
|
|
|
|
// AE2: variable assignment — concrete to protocol variable (no xx)
|
|
{
|
|
acc := Accumulator.{ total = 0 };
|
|
a: Adder = acc;
|
|
a.add(5);
|
|
a.add(3);
|
|
print("AE2: {}\n", a.value());
|
|
}
|
|
|
|
// AE3: struct literal passed directly (no xx)
|
|
{
|
|
use_counter :: (c: Counter) -> s32 { c.inc(); c.inc(); c.get(); }
|
|
result := use_counter(SimpleCounter.{ val = 100 });
|
|
print("AE3: {}\n", result);
|
|
}
|
|
|
|
// AE4: explicit xx still works (not broken)
|
|
{
|
|
use_counter :: (c: Counter) -> s32 { c.inc(); c.get(); }
|
|
result := use_counter(xx SimpleCounter.{ val = 50 });
|
|
print("AE4: {}\n", result);
|
|
}
|
|
|
|
// AE5: pointer auto-erasure — *ConcreteType to protocol
|
|
{
|
|
use_adder :: (a: Adder) { a.add(10); }
|
|
acc := Accumulator.{ total = 5 };
|
|
p := @acc;
|
|
use_adder(p);
|
|
print("AE5: {}\n", acc.total);
|
|
}
|
|
}
|