import std::io; # A type parameter known to be an instance of a generic taking a pack has the # generic's shape, its pack the type arguments from there on, and the shape's # types are bounded as the generic bounds its parameters. One bounded by # `pointer` is a `*T::target`, which `*x` reads, and writes where `if const # T::mutable` says it does. type strip_pointer = match const T { pointer => strip_pointer, else => T }; struct tagged where H: integer { var head: H; } def tagged::first(&this): H { return this.head; } struct pair where A: ordered { var a: A; var b: B; } def pair::smaller(&this, other: A): bool { return this.a < other; } def head_of(x: &T): u64 { if const T is tagged { return x.first() as u64 + T::args.length; } return 0; } def less(x: &T): bool { if const T is pair { return x.smaller(x.a); } return false; } def read(x: T): T::target where T: pointer { return *x; } def write(x: T, v: T::target): bool where T: pointer { if const T::mutable { *x = v; return true; } return false; } def stripped(x: T): strip_pointer { if const T is pointer { return stripped(*x); } return x; } def main(): void { val t := tagged::{ head = 7 }; val p := pair::{ a = 3, b = true }; var n: i32 = 4; val q: *mut i32 = &mut n; val qq: **mut i32 = &q; val r: *i32 = &n; println($"{head_of(&t)} {head_of(&n)} {less(&p)} {read(q)} {write(q, 9)} {write(r, 1)} {n} {stripped(qq)}"); }