Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get value from record with supplying default #1920

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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