aboutsummaryrefslogtreecommitdiff
path: root/docs/tables.md
blob: 2f07436a30d2caf8735bf01f7462b411755be25c (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Tables

Tables are Tomo's associative mapping structure, also known as a Dictionary or
Map. Tables are efficiently implemented as a hash table that preserves
insertion order and has fast access to keys and values as list slices. Tables
support *all* types as both keys and values.

## Syntax

Tables are written using `{}` curly braces with `=` equals signs associating key
expressions with value expressions and commas between entries:

```tomo
table := {"A"=10, "B"=20}
```

Empty tables must specify the key and value types explicitly:

```tomo
empty : {Text=Int} = {}
```

For type annotations, a table that maps keys with type `K` to values of type
`V` is written as `{K=V}`.

### Comprehensions

Similar to lists, tables can use comprehensions to dynamically construct tables:

```tomo
t := {i=10*i for i in 10}
t := {i=10*i for i in 10 if i mod 2 == 0}
t := {-1=-10, i=10*i for i in 10}
```

## Accessing Values

Table values can be accessed with square bracket indexing. The result is an
optional value:

```tomo
table := {"A"=1, "B"=2}
>> table["A"]
= 1?
>> table["missing"]
= none
```

As with all optional values, you can use the `!` postfix operator to assert
that the value is non-none (and create a runtime error if it is), or you can
use the `or` operator to provide a fallback value in the case that it's none:

```tomo
>> table["A"]!
= 1

>> table["missing"] or -1
= -1
```

### Fallback Tables

Tables can specify a fallback table that is used when looking up a value if it
is not found in the table itself:

```tomo
t := {"A"=10}
t2 := {"B"=20; fallback=t}
>> t2["A"]
= 10?
```

The fallback is available by the `.fallback` field, which returns an optional
table value:

```tomo
>> t2.fallback
= {"A"=10}?
>> t.fallback
= none
```

### Default Values

Tables can specify a default value which will be returned if a value is not
present in the table or its fallback (if any).

```tomo
counts := &{"foo"=12; default=0}
>> counts["foo"]
= 12
>> counts["baz"]
= 0
counts["baz"] += 1
>> counts["baz"]
= 1
```

When values are accessed from a table with a default value, the return type
is non-optional (because a value will always be present).

## Setting Values

You can assign a new key/value mapping or overwrite an existing one using
`.set(key, value)` or an `=` assignment statement:

```tomo
t := {"A"=1, "B"=2}
t["B"] = 222
t["C"] = 333
>> t
= {"A"=1, "B"=222, "C"=333}
```

## Length

Table length can be accessed by the `.length` field:

```tomo
>> {"A"=10, "B"=20}.length
= 2
```

## Accessing Keys and Values

The keys and values of a table can be efficiently accessed as lists using a
constant-time immutable slice of the internal data from the table:

```tomo
t := {"A"=10, "B"=20}
>> t.keys
= ["A", "B"]
>> t.values
= [10, 20]
```

## Iteration

You can iterate over the key/value pairs in a table like this:

```tomo
for key, value in table
    ...

for key in table
    ...
```

Table iteration operates over the value of the table when the loop began, so
modifying the table during iteration is safe and will not result in the loop
iterating over any of the new values.

# API

[API documentation](../api/tables.md)