namespace; include rt, run; // types: structs, a generic container, and looking at it all through reflection. // a pointer type has its star after the type, like c: int*, point*, vec*. typecheck on; struct point { int x, y; }; struct vec { int len, cap; T* items; }; vec*.u mkvec(int cap) { vec* v = rt.new(vec); v.cap = cap; v.items = rt.new(T, cap); return v; } // x is given away (.d): the vector's memory holds it from now on, and the // collector looks after it bool push(vec* v, T.d x) { if (v.len == v.cap) { rt.free(x); return 0; } v.items[v.len] = d*x; v.len++; return 1; } void show(word t) { let i = 0; run.tprint(t); rt.put_s(" is "); rt.put_n(run.tsize(t)); rt.put_s(" words: "); while (i < run.tnfields(t)) { if (i) rt.put_s(", "); rt.put_s(run.tfname(t, i)); rt.put_s(" "); run.tprint(run.tftype(t, i)); i++; } rt.put_c(10); return; } int main() { vec* v = mkvec(4); int i = 0; while (i < 3) { point* p = rt.new(point); p.x = i; p.y = i * i; push(v, p); i++; } rt.put_s("last: "); rt.put_n(v.items[2].y); rt.put_c(10); show(typeof(point)); show(typeof(*v)); rt.put_s("push is "); run.tprint(run.sigof(push)); rt.put_c(10); // each set of type arguments gets its own copy of mkvec, so what it stamped is vec rt.put_s("the heap says v is a "); run.tprint(rt.typeof_obj(v)); rt.put_s(", and v.items[0] is a "); run.tprint(rt.typeof_obj(v.items[0])); rt.put_c(10); return 0; }