mirror of
https://github.com/apple/pkl.git
synced 2026-05-28 01:29:15 +02:00
Add getOrDefault method to Listing and Mapping (#1053)
This PR adds methods to Listing and Mapping that can be used to retrieve members. If the member for the index/key isn't present, it applies default to the index/key. In both cases, this is essentially sugar for getOrNull(<index/key>) ?? default.apply(<index/key>). Co-authored-by: Daniel Chao <daniel.h.chao@gmail.com>
This commit is contained in:
@@ -29,3 +29,7 @@ res4 = (res3) {
|
||||
name = "Barn Owl"
|
||||
}
|
||||
}
|
||||
|
||||
res5 = (res4.getOrDefault(99)) {
|
||||
name = "Bald Eagle"
|
||||
}
|
||||
|
||||
@@ -38,3 +38,7 @@ res3: Mapping<String, Person> = new {
|
||||
age = 60
|
||||
}
|
||||
}
|
||||
|
||||
res4 = (res3.getOrDefault("Bald Eagle")) {
|
||||
age = 99
|
||||
}
|
||||
|
||||
@@ -38,3 +38,7 @@ res4 {
|
||||
age = 5
|
||||
}
|
||||
}
|
||||
res5 {
|
||||
name = "Bald Eagle"
|
||||
age = 99
|
||||
}
|
||||
|
||||
@@ -32,3 +32,7 @@ res3 {
|
||||
age = 60
|
||||
}
|
||||
}
|
||||
res4 {
|
||||
name = "Bald Eagle"
|
||||
age = 99
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user