import std::io; # A struct's field written `items: Ts...` holds a value of each of the pack's # types, laid out in order as fields are. It is used as a variadic parameter's # pack is: `items[i]` at an index known while compiling, `items.length`, # `for const`, and `...items`, which moves the values that close to the # function given them, leaving the field empty. A literal gives it a pack of # its types whole, moving what closes into it, and one leaving it out zeroes # it. A new value's pack, as a call's result's, gives up what it holds. What # of it closes is closed where the struct is, the last first. `core`'s `tuple` # is a struct of one, with `tuple::of(...)` and `t.at::()`. struct bundle { var items: Ts...; } def bundle::of(items: Ts...): bundle { return bundle { items = items }; } def bundle::first(&this): Ts...[0] where Ts...: zeroable { return this.items[0]; } struct tagged { var tag: T; var rest: Ts...; var after: u8; } struct noisy : owning { var id: u8; } def noisy::close(&mut this): void { if this.id != 0 { println($"close {this.id}"); } } def sum3(a: i64, b: i64, c: i64): i64 { return a + b + c; } def consume(a: noisy, b: i64, c: noisy): void { println($"consumed {a.id} {b} {c.id}"); } def passed(t: tuple): u64 { return t.items[0] as u64 + t.items[1]; } def made(): tuple { return tuple::of(1 as u8, 2 as u64, 3 as u8); } def described(t: &tuple): string { return $"{t.at::<0>()} {t.at::<1>()}"; } def pair_of(a: u8, b: u8): tuple { return tuple::of(noisy { id = a }, 0, noisy { id = b }); } def main(): void { var t := tuple::of(1, true, 7u); println($"{t.items[0]} {t.items[1]} {t.items[2]} {t.items.length} {size_of::>()}"); t.at::<0>() += 41; t.items[1] = false; for const x in t.items { println($"each {x}"); } var p := tuple::of(4, true); p.at::<0>() *= 5; println($"{described(&p)} {sum3(...tuple::of(1, 2, 3).items)}"); var z: tuple; val e := tuple::of(); println($"{z.items[0]} {z.items[1]} {e.items.length}"); val nested := bundle::of(bundle::of(1, 2), true, tuple::of(3 as u8)); println($"{nested.items[0].items[1]} {nested.items[2].items[0]} {nested.first().items.length}"); val g := tagged::{ tag = 9, rest = tuple::of(4 as u8, 5 as i32).items, after = 6 }; println($"{g.tag} {g.rest[0]} {g.rest[1]} {g.after} {size_of::>()}"); var c := tuple::of(1, 2); val d := c; c.items[0] = 10; println($"{c.items[0]} {d.items[0]} {passed(tuple::of(1 as u8, 2 as u64))} {made().items[2]}"); var h := tuple::of(noisy { id = 1 }, 5, noisy { id = 2 }); h = tuple::of(noisy { id = 3 }, 6, noisy { id = 4 }); println("written over"); consume(...h.items); println("moved"); val k := tuple::of(noisy { id = 7 }, 8, noisy { id = 9 }); println($"{k.at::<1>()}"); # A new value's pack gives up what it holds, as the value would. consume(...pair_of(10, 11).items); val kept := bundle { items = pair_of(12, 13).items }; println($"kept {kept.items[2].id}"); }