sclass Chain implements Iterable { A element; Chain next; int size; *() {} *(A *element) { size = 1; } *(A *element, Chain *next) { size = next != null ? next.size+1 : 1; } toString { ret str(toList()); } ArrayList toList() { ArrayList l = emptyList(size); Chain c = this; while (c != null) { l.add(c.element); c = c.next; } ret l; } // TODO: optimize public Iterator iterator() { ret toList().iterator(); } }