code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(46 lines)
1 '\" t
2 .\" Copyright (c) 2026 Bruce Hill
3 .\" All rights reserved.
4 .\"
5 .TH Table.get_or_set 3 2026-03-08 "Tomo man-pages"
6 .SH NAME
7 Table.get_or_set \- get an item or set a default if absent
8 .SH LIBRARY
9 Tomo Standard Library
10 .SH SYNOPSIS
11 .nf
12 .BI Table.get_or_set\ :\ func(t:\ &{K:V},\ key:\ K,\ default:\ V\ ->\ V?)
13 .fi
14 .SH DESCRIPTION
15 If the given key is in the table, return the associated value. Otherwise, insert the given default value into the table and return it.
18 .SH ARGUMENTS
20 .TS
21 allbox;
22 lb lb lbx
23 l l l.
24 Name Type Description
25 t &{K:V} The table.
26 key K The key whose associated value is to be retrieved.
27 default V The default value to insert and return if the key is not present in the table.
28 .TE
29 .SH RETURN
30 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.
32 .SH NOTES
33 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.
34 The default value is only evaluated if the key is missing.
36 .SH EXAMPLES
37 .EX
38 t := &{"A": @[1, 2, 3]; default=@[]}
39 t.get_or_set("A").insert(4)
40 t.get_or_set("B").insert(99)
41 assert t["A"][] == [1, 2, 3, 4]
42 assert t["B"][] == [99]
43 assert t.get_or_set("C", @[0, 0, 0])[] == [0, 0, 0]
44 .EE
45 .SH SEE ALSO
46 .BR Tomo-Table (3)