(46 lines)
1 '\" t2 .\" Copyright (c) 2026 Bruce Hill3 .\" All rights reserved.4 .\"5 .TH Table.get_or_set 3 2026-03-08 "Tomo man-pages"6 .SH NAME7 Table.get_or_set \- get an item or set a default if absent8 .SH LIBRARY9 Tomo Standard Library10 .SH SYNOPSIS11 .nf12 .BI Table.get_or_set\ :\ func(t:\ &{K:V},\ key:\ K,\ default:\ V\ ->\ V?)13 .fi14 .SH DESCRIPTION15 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 ARGUMENTS20 .TS21 allbox;22 lb lb lbx23 l l l.24 Name Type Description25 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 .TE29 .SH RETURN30 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 NOTES33 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 EXAMPLES37 .EX38 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 .EE45 .SH SEE ALSO46 .BR Tomo-Table (3)