








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









mentions y int y = 137; struct f_env { int y; } int f(T x, f_env env) { return e; } struct f_env f_env1; f_env1.y = y; … f(x, &f_env1); … struct T_T’_closure { T’ ()(T, void *) funptr; void *env; }; struct T_T’_closure f_closure; f_closure.funptr = (…)f; f_closure.env =(void *)&f_env1; … g(&f_closure); use env->y Type system doesn’t understand
Array.iter: (‘a->unit) -> ’a array -> unit
private void preloadNext() { loop: while (true) { switch (state) { case 1: case 3: hasNext = true; if (subiterator.hasNext()) { current = subiterator.next(); return; } else { if (state == 1) { state = 2; current = RBTree.this.value; return; } else { hasNext = false; return; } case 2: subiterator=RBTree.right.iterator(); state = 3; continue loop; } class TreeIterator implements Iterator { Iterator subiterator; boolean hasNext; Object current; int state; // states: // 1. Iterating through left child. // 2. Just yielded current node value // 3. Iterating through right child TreeIterator() { subiterator=RBTree.this.left.iterator(); state = 1; preloadNext(); } public boolean hasNext() { return hasNext; } public Object next() { if (!hasNext) throw new NoSuchElementException(); Object ret = current; preloadNext(); return ret; }
Polymorphism and parameterized types
let f (x:’a array) (print_x:‘a -> unit) = … print_x(x.(i)) … type ‘a pair = ‘a * ‘a … let z: foo pair = … template void f(A x[], void (*print_x)(A)) { … print_x(x[i])… } template class Pair { A left; A right; } Pair z = … public void f(A[] x, Printer p) { … p.doit(x[i]); } class Pair { A left; A right; } Pair z = … Warning: may fail. C++ doesn’t restrict how A is used in Pair. Modularity failure.
f(void x[], void (print_x)(void *)) { …}
typedef struct nat_s *Nat; // an abstract type! Nat create_nat(int num, int den);
if (o instanceof Foo) { Foo asFoo = (Foo)o; int x = asFoo.x; int y = asFoo.y; … } else if (o instanceof Bar) { Bar asBar = (Bar)o; … } match o with Foo(x,y) -> … | Bar(h::Baz(z)) -> …
struct variant_s { int tag; union { struct foo f; // if tag== struct bar b; // if tag== } }