Reputation: 1295
I frequently run into a situation where I am testing for the existence of a value in a nested map or array. To prevent verbose conditionals, I would like to simplify the code to not test for existence at each level of the node, instead going right after what I want.
For example:
local(mymap = map('a' = (:1,2,3), 'b' = (:4,5,6)))
if (#mymap->find('c')->contains(9) ) => {}
If key 'c' does not exist in #mymap, then the contains() method throws an error.
Would it be foolish of me to define this in Lasso Startup?
define void->contains(...) => false
That would allow the above conditional to work, without having to add compound expressions to first test if 'c' exists. Am I missing some unintended consequences? Am I overlooking a more efficient way to do this?
Upvotes: 1
Views: 54
Reputation: 308
The way I go about it is to use an "or":
if((#mymap->find('c') || (:)) >> 9) => {}
What happens here is that if #mymap->find('c')
produces a non-false value, it's used for the contains, otherwise the empty staticarray is use for the contains.
Upvotes: 1