Skip to content

Commit

Permalink
Add get field value from record with default
Browse files Browse the repository at this point in the history
* Format getor function and add example
  • Loading branch information
olorin37 committed May 21, 2024
1 parent 8d88d63 commit ea41f82
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/stdlib/std.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,28 @@
"%%
= 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 r =>
if %has_field% field r then
r."%{field}"
else
default_value,

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

0 comments on commit ea41f82

Please sign in to comment.