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:
Jen Basch
2025-07-22 15:45:49 -07:00
committed by GitHub
parent 306a3b0fc2
commit 85e4f133a4
7 changed files with 65 additions and 2 deletions
@@ -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
}