aboutsummaryrefslogtreecommitdiff
path: root/examples/struct_closure_opt.zc
diff options
context:
space:
mode:
Diffstat (limited to 'examples/struct_closure_opt.zc')
-rw-r--r--examples/struct_closure_opt.zc25
1 files changed, 14 insertions, 11 deletions
diff --git a/examples/struct_closure_opt.zc b/examples/struct_closure_opt.zc
index 67315bf..254ff07 100644
--- a/examples/struct_closure_opt.zc
+++ b/examples/struct_closure_opt.zc
@@ -1,3 +1,6 @@
+namespace;
+include rt;
+
// struct methods, closures and ownership in one file, written so the
// compiler's hints have something to say about it. it runs as it is.
@@ -12,36 +15,36 @@ struct point {
}
}
-fn sink(.d p) { free(d*p); return 0; }
+void sink(.d p) { rt.free(p); return; }
fn features() {
- // a closure copies the variables it uses when it is made
+ // a closure shares the variables it captures
let base = 100;
let add = fn(a, b) { return a + b + base; };
let total = add(10, 20);
- let p1 = new(point);
+ let p1 = rt.new(point);
p1.x = 5; p1.y = 12;
- let p2 = new(point);
+ let p2 = rt.new(point);
p2.x = 0; p2.y = 0;
// made and given away on the next line, so it never needed a name
- let tmp = new(2);
- sink(d*tmp);
+ let tmp = rt.new(2);
+ sink(tmp);
// allocated again on every pass of the loop
let i = 0;
while (i < 3) {
- let scratch = new(10);
+ let scratch = rt.new(10);
scratch[0] = i;
- sink(d*scratch);
+ sink(scratch);
i++;
}
return total + p1.dist(p2);
}
-fn main() {
- put_n(features()); put_c(10);
- return 0;
+void main() {
+ rt.put_n(features()); rt.put_c(10);
+ return;
}