ChaiScript/unittests/map_find.chai
leftibot dcdab50efd
Fix #470: Expose find() for associative container types (#668)
Add a native find() method to unique associative containers (e.g., Map)
that returns the mapped value if the key exists, or an undefined
Boxed_Value if not. This allows checking for key existence without
mutating the container (unlike operator[]) or requiring exception
handling (unlike at()).

Co-authored-by: leftibot <leftibot@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 18:57:26 -06:00

16 lines
319 B
ChaiScript

auto m = ["k":"v"]
// find existing key returns the value
assert_equal("v", m.find("k"))
// find missing key returns undef
assert_true(m.find("missing").is_var_undef())
// find does not mutate the map
m.find("other")
assert_equal(1, m.size())
// find works with in-place map
assert_equal("v", ["k":"v"].find("k"))