2012-01-05 10:44:59 -08:00
|
|
|
use std;
|
|
|
|
|
2012-01-09 14:53:13 +01:00
|
|
|
import comm::chan;
|
|
|
|
import comm::send;
|
2012-01-05 10:44:59 -08:00
|
|
|
|
|
|
|
fn main() { test05(); }
|
|
|
|
|
2012-01-11 09:58:05 -08:00
|
|
|
fn mk_counter<A:copy>() -> fn~(A) -> (A,uint) {
|
2012-01-05 10:44:59 -08:00
|
|
|
// The only reason that the counter is generic is so that it closes
|
|
|
|
// over both a type descriptor and some data.
|
2012-06-25 20:00:46 -07:00
|
|
|
let v = [mut 0u]/~;
|
2012-01-11 09:58:05 -08:00
|
|
|
ret fn~(a: A) -> (A,uint) {
|
2012-01-05 10:44:59 -08:00
|
|
|
let n = v[0];
|
|
|
|
v[0] = n + 1u;
|
|
|
|
(a, n)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test05() {
|
|
|
|
let fp0 = mk_counter::<float>();
|
|
|
|
|
|
|
|
assert (5.3f, 0u) == fp0(5.3f);
|
|
|
|
assert (5.5f, 1u) == fp0(5.5f);
|
|
|
|
|
|
|
|
let fp1 = copy fp0;
|
|
|
|
|
|
|
|
assert (5.3f, 2u) == fp0(5.3f);
|
|
|
|
assert (5.3f, 2u) == fp1(5.3f);
|
|
|
|
assert (5.5f, 3u) == fp0(5.5f);
|
|
|
|
assert (5.5f, 3u) == fp1(5.5f);
|
|
|
|
}
|