Files
bux-lang/examples/struct_tuple_pat.bux
T
dimgigov e3ca724bfa feat: struct and tuple patterns in match (bootstrap + selfhost)
Support destructuring in match arms:
- Tuple: (a, b) binds subject._0 / _1
- Struct: Point { x: px, y: py } and shorthand Point { x, y }

Bootstrap: matchPatternBindings for pkTuple/pkStruct; register local
tuple typedefs from function bodies. Selfhost: parse, Sema_BindPattern,
Lcx_PatternBindings with scope defines. Fix operator-overload path that
crashed when typeName was null after pattern binds.

Example: examples/struct_tuple_pat.bux. Selfhost-loop IDENTICAL.
2026-07-18 01:15:44 +03:00

54 lines
1.0 KiB
Plaintext

// Struct and tuple patterns in match: (a, b), Point { x, y }
import Std::Io::{PrintLine, PrintInt};
import Std::Test::{Test_AssertEqInt, Test_Pass};
struct Point {
x: int,
y: int,
}
func SumPair(t: (int, int)) -> int {
match t {
(a, b) => a + b,
_ => 0
}
}
func PointCode(p: Point) -> int {
match p {
Point { x: px, y: py } => px * 10 + py,
_ => -1
}
}
func PointShorthand(p: Point) -> int {
// { x, y } means { x: x, y: y }
match p {
Point { x, y } => x + y,
_ => 0
}
}
func Main() -> int {
let t: (int, int) = (10, 20);
Test_AssertEqInt(SumPair(t), 30);
let lit: (int, int) = (7, 8);
let s: int = match lit {
(a, b) => a * b,
_ => 0
};
Test_AssertEqInt(s, 56);
let p: Point = Point { x: 3, y: 4 };
Test_AssertEqInt(PointCode(p), 34);
Test_AssertEqInt(PointShorthand(p), 7);
PrintInt(SumPair(t));
PrintLine("");
PrintInt(PointCode(p));
PrintLine("");
Test_Pass("struct_tuple_pat");
return 0;
}