Skip to content

Commit ce7f775

Browse files
committed
Add get field value from record with default
* Format getor function and add example
1 parent 8d88d63 commit ce7f775

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

core/stdlib/std.ncl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,28 @@
21482148
"%%
21492149
= fun field r => r."%{field}",
21502150

2151+
getor
2152+
: forall a. String -> a -> { _ : a } -> a
2153+
| doc m%%"
2154+
Returns the field of a record with given name or default value,
2155+
if there is no such field.
2156+
2157+
# Examples
2158+
2159+
```nickel
2160+
std.record.get "tree" 3 { one = 1, two = 2 } =>
2161+
3
2162+
2163+
std.record.get "one" 11 { one = 1, two = 2 } =>
2164+
1
2165+
```
2166+
"%%
2167+
= fun field default_value r =>
2168+
if std.record.has_field field r then
2169+
std.record.get field r
2170+
else
2171+
default_value,
2172+
21512173
insert
21522174
: forall a. String -> a -> { _ : a } -> { _ : a }
21532175
| doc m%%"

0 commit comments

Comments
 (0)