v / examples
Raw file | 27 loc (23 sloc) | 573 bytes | Latest commit hash 017ace6ea
1type Tree = Empty | Node
2
3struct Empty {}
4
5struct Node {
6 value int
7 left Tree
8 right Tree
9}
10
11// Note: a match expression, infers the type of its result
12// from the type of the return value in the first branch,
13// => it needs an explicit int(0) cast here:
14fn size(tree Tree) int {
15 return match tree {
16 Empty { int(0) }
17 Node { 1 + size(tree.left) + size(tree.right) }
18 }
19}
20
21fn main() {
22 node1 := Node{30, Empty{}, Empty{}}
23 node2 := Node{20, Empty{}, Empty{}}
24 tree := Node{10, node1, node2}
25 println('tree structure:\n ${tree}')
26 println('tree size: ${size(tree)}')
27}