v
Зеркало из https://github.com/vlang/v
1type Tree = Empty | Node
2
3struct Empty {}
4
5struct Node {
6value int
7left Tree
8right 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 {
15return match tree {
16Empty { int(0) }
17Node { 1 + size(tree.left) + size(tree.right) }
18}
19}
20
21fn main() {
22node1 := Node{30, Empty{}, Empty{}}
23node2 := Node{20, Empty{}, Empty{}}
24tree := Node{10, node1, node2}
25println('tree structure:\n ${tree}')
26println('tree size: ${size(tree)}')
27}
28