aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/tagenum.zc
blob: 013f19127ec1c46e3d3eeb8a5ce0f76630a408bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace;
include rt;

// tagged enums: a member can carry fields, a constructor builds it, and a
// switch matches on the member and binds its fields
enum shape { circle { int r; }, rect { int w; int h; }, dot };

fn area(shape* s) {
	switch (s) {
	case circle(r): return r * r * 3;
	case rect(w, h): return w * h;
	case dot: return 0;
	}
	return -1;
}

fn main() {
	shape* a = shape.circle(3);
	shape* b = shape.rect(4, 5);
	shape* c = shape.dot();
	rt.put_n(area(a)); rt.put_c(32);
	rt.put_n(area(b)); rt.put_c(32);
	rt.put_n(area(c)); rt.put_c(32);
	rt.put_n(a.tag); rt.put_c(32);
	rt.put_n(b.tag); rt.put_c(32);
	rt.put_n(c.tag); rt.put_c(10);
	rt.free(d*a); rt.free(d*b); rt.free(d*c);
	return 0;
}