| 1 | module FirstUse; |
| 2 | import Out, Str, Table := Table([]const U8, Colour, Str.hash, Str.eq), |
| 3 | Ints := Pile(I64), More := Pile(I64), Never := Loud(I64), Ones := Loud(U8); |
| 4 | import SYSTEM; |
| 5 | |
| 6 | type Colour = (red, green, blue); |
| 7 | |
| 8 | var names: Table.Table; (* Table is made here, after Colour *) |
| 9 | |
| 10 | proc show(k: []const U8); |
| 11 | var c: Colour; found: BOOL; |
| 12 | begin |
| 13 | c, found := Table.get(SYSTEM.ADR(names), k); |
| 14 | Out.str(k); |
| 15 | if not found then Out.str(" -"); elsif c = red then Out.str(" red"); else Out.str(" not red"); end; |
| 16 | Out.ln; |
| 17 | end; |
| 18 | |
| 19 | (* Ones is first used in a procedure, and made before its code *) |
| 20 | proc main*; |
| 21 | var a: Ints.Pile; b: More.Pile; |
| 22 | begin |
| 23 | a := b; (* two imports, one instance *) |
| 24 | if a.len = 0 then Out.str("an empty list\n"); end; |
| 25 | Out.str("in main, Ones has size "); Out.int(Ones.size); Out.ln; |
| 26 | show("sky"); |
| 27 | show("rose"); |
| 28 | show("grass"); |
| 29 | end; |
| 30 | |
| 31 | begin |
| 32 | _ := Table.put(SYSTEM.ADR(names), "rose", red); |
| 33 | _ := Table.put(SYSTEM.ADR(names), "sky", blue); |
| 34 | end FirstUse. |