aboutsummaryrefslogtreecommitdiff
path: root/docs/tables.md
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-12 00:55:43 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-12 00:55:43 -0400
commit327d466b9543aee3983277ca4158e5b7aa06fdf8 (patch)
tree64028cc0ca38b76461a85be02e3103db600f0bcc /docs/tables.md
parentfa7e52787f81f3673f9d57e9e4ba7fc015ca8432 (diff)
Table:get() now uses optional values instead of default or failure modes
Diffstat (limited to 'docs/tables.md')
-rw-r--r--docs/tables.md63
1 files changed, 27 insertions, 36 deletions
diff --git a/docs/tables.md b/docs/tables.md
index b18b6209..9de07b67 100644
--- a/docs/tables.md
+++ b/docs/tables.md
@@ -38,6 +38,23 @@ t := {i: 10*i for i in 10 if i mod 2 == 0}
t := {-1:-10, i: 10*i for i in 10}
```
+### Getting Values
+
+To get a value from a table, use `:get(key)`, which returns an _optional_
+value, depending on whether it was present in the table or not. For convenience,
+you can use the `!` postifx operator to perform a check to ensure that the value
+was found or error if it wasn't:
+
+```tomo
+>> t := {"x":1, "y":2}
+>> t:get("x")
+= 1?
+>> t:get("????")
+= !Int
+>> t:get("x")!
+= 1
+```
+
### Fallback Tables
Tables can specify a fallback table that is used when looking up a value if it
@@ -151,61 +168,35 @@ Nothing.
### `get`
**Description:**
-Retrieves the value associated with a key, or returns a default value if the key is not present.
+Retrieves the value associated with a key, or returns null if the key is not present.
**Usage:**
```markdown
-t:get(key: K, default: V) -> V
+t:get(key: K) -> V?
```
**Parameters:**
- `t`: The table.
- `key`: The key whose associated value is to be retrieved.
-- `default`: The value to return if the key is not present. If this argument is
- not provided, a runtime error will be created if the key is not present.
**Returns:**
-The value associated with the key or the default value if the key is not found.
+The value associated with the key or null if the key is not found.
**Example:**
```markdown
>> t := {"A":1, "B":2}
>> t:get("A")
-= 1
+= 1? : Int?
->> t:get("xxx", 0)
-= 0
-```
-
----
+>> t:get("????")
+= !Int : Int?
-### `get_or_null`
+>> t:get("A")!
+= 1 : Int
-**Description:**
-Retrieves the value associated with a key, or returns `null` if the key is not present.
-This method is only available on tables whose values are pointers.
-
-**Usage:**
-```markdown
-t:get_or_null(key: K) -> @V?
-```
-
-**Parameters:**
-
-- `t`: The table.
-- `key`: The key whose associated value is to be retrieved.
-
-**Returns:**
-A mutable reference to the value associated with the key or `null` if the key is not found.
-
-**Example:**
-```markdown
->> t := {"A": @[10]}
->> t:get_or_null("A")
-= @[10]?
->> t:get_or_null("xxx")
-= !@[Int]
+>> t:get("????"):or_else(0)
+= 0 : Int
```
---