New key-value function: get(kv, k, default) #2077
nahiyan-yasaar-selise
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently, the behaviour of kv[k] is to raise an error when a key is not found. To avoid this, we are forced to create a separate array which has a copy of all the keys available in the kv, after which we can use the ternary operator in an expression node:
x = has(arr, k1) ? kv[k1] : 0
. For a single item it's not a big deal, but we have many such items (e.g.y
checks fork2
andz
checks fork3
etc).We would like the ability for it to return
null
instead. Since error handling is important, the current default behaviour for this syntax (kv[k]
) can stay as is. Instead, it would be better to have a separate function so we can dox = get(kv, k1) ? get(kv, k1) : 0
.Even better is if we have an optional parameter for returning default values when a key isn't found, so we can simplify it even more to
x = get(kv, k1, 0)
For reference, this is identical to python's
dict[key]
(raises error for missing key) syntax vsdict.get(key, default)
syntax.Beta Was this translation helpful? Give feedback.
All reactions