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
|
'\" t
.\" Copyright (c) 2025 Bruce Hill
.\" All rights reserved.
.\"
.TH Table.get_or_set 3 2025-04-21T14:44:34.263808 "Tomo man-pages"
.SH NAME
Table.get_or_set \- get an item or set a default if absent
.SH LIBRARY
Tomo Standard Library
.SH SYNOPSIS
.nf
.BI Table.get_or_set\ :\ func(t:\ &{K=V},\ key:\ K,\ default:\ V\ ->\ V?)
.fi
.SH DESCRIPTION
If the given key is in the table, return the associated value. Otherwise, insert the given default value into the table and return it.
.SH ARGUMENTS
.TS
allbox;
lb lb lbx lb
l l l l.
Name Type Description Default
t &{K=V} The table. -
key K The key whose associated value is to be retrieved. -
default V The default value to insert and return if the key is not present in the table. -
.TE
.SH RETURN
Either the value associated with the key (if present) or the default value. The table will be mutated if the key is not already present.
.SH NOTES
If no default value is provided explicitly, but the table has a default value associated with it, the table's default value will be used.
The default value is only evaluated if the key is missing.
.SH EXAMPLES
.EX
>> t := &{"A"=@[1, 2, 3]; default=@[]}
>> t.get_or_set("A").insert(4)
>> t.get_or_set("B").insert(99)
>> t
= &{"A"=@[1, 2, 3, 4], "B"=@[99]}
>> t.get_or_set("C", @[0, 0, 0])
= @[0, 0, 0]
>> t
= &{"A"=@[1, 2, 3, 4], "B"=@[99], "C"=@[0, 0, 0]}
.EE
|