| 1 | module VariantConst; |
| 2 | import Out; |
| 3 | |
| 4 | type |
| 5 | Shape = variant circle(r: F64); rect(w, h: INT); dot; named(name: []const U8; n: U8); end; |
| 6 | Rule = record tag: INT; s: Shape; end; |
| 7 | |
| 8 | const |
| 9 | Shapes: []Shape = [circle(1.5), rect(2, 3), dot, named("box", 7)]; |
| 10 | One: Shape = rect(4, 5); |
| 11 | Rules: [2]Rule = [{tag: 1, s: dot}, {tag: 2, s: named("x", 1)}]; |
| 12 | |
| 13 | proc show(const s: Shape); |
| 14 | begin |
| 15 | match s of |
| 16 | | circle(r): Out.str("circle "); Out.int(I64(r * 2.0)); |
| 17 | | rect(w, h): Out.str("rect "); Out.int(w); Out.char(' '); Out.int(h); |
| 18 | | dot: Out.str("dot"); |
| 19 | | named(name, n): Out.str(name); Out.char(' '); Out.int(I64(n)); |
| 20 | end; |
| 21 | Out.ln; |
| 22 | end; |
| 23 | |
| 24 | proc main*; |
| 25 | var i: INT; s: Shape; |
| 26 | begin |
| 27 | for i := 0 to LEN(Shapes) - 1 do show(Shapes[i]); end; |
| 28 | show(One); |
| 29 | show(Rules[1].s); |
| 30 | s := One; |
| 31 | show(s); |
| 32 | match One of |
| 33 | | rect(w, h): Out.int(w * h); Out.ln; |
| 34 | | else: |
| 35 | end; |
| 36 | end; |
| 37 | |
| 38 | end VariantConst. |