import std::io; # A property named as a value is its `get`, a function of `&this`, and a method # or property of a generic type named without its type arguments takes them # from the function type expected, or from a parameter's `invokable` bound, as # an adapter's is. struct rect { var w: f32; var h: f32; } val rect::area: f32 { get => this.w * this.h; } var rect::width: f32 { get => this.w; set => this.w = value; } union outcome { var failed: strview; var made: T; } val outcome::ok: bool { get => this is made; } def outcome::take(&this): T { return this.made; } def read(text: &strview): outcome { return "not a digit" if text.length != 1 || text[0] < '0' || text[0] > '9'; return (text[0] - '0') as i64; } def doubled(xs: span>): vector where T: integer { return xs.iter().copied().filter(outcome::ok).map(def(o) => o.take() * 2).to_vector(); } def main(): void { var r := rect { w = 3, h = 4 }; val area: def(&rect): f32 = rect::area; val width: def(&rect): f32 = rect::width; r.width = 5; println($"{area(&r)} {width(&r)}"); val texts: strview[5] = ["1", "x", "7", "", "3"]; val read_all := texts[:].iter().map(read).to_vector(); val kept := read_all.view().iter().copied().filter(outcome::ok).map(def(o) => o.take()).to_vector(); println($"{kept.length}: {kept[0]} {kept[1]} {kept[2]}"); val take: def(&outcome): i64 = outcome::take; val first: outcome = 9; println($"{take(&first)}"); val twice := doubled(read_all.view()); println($"{twice[0]} {twice[1]} {twice[2]}"); }