Skip to content

Commit

Permalink
Add get field value from record with default
Browse files Browse the repository at this point in the history
  • Loading branch information
olorin37 committed May 23, 2024
1 parent 28370ef commit 7d7fc5a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/stdlib/std.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,27 @@
"%%
= fun field r => r."%{field}",

get_or
: forall a. String -> a -> { _ : a } -> a
| doc m%%"
Returns the field of a record with given name or default value,
if there is no such field.

# Examples

```nickel
std.record.get_or "tree" 3 { one = 1, two = 2 }
=> 3
std.record.get_or "one" 11 { one = 1, two = 2 }
=> 1
```
"%%
= fun field default_value record =>
if %has_field% field record then
record."%{field}"
else
default_value,

insert
: forall a. String -> a -> { _ : a } -> { _ : a }
| doc m%%"
Expand Down

0 comments on commit 7d7fc5a

Please sign in to comment.