jsonnet
jsonnet copied to clipboard
Safe select operator
It would be useful to have a selector notation that was null safe. Perhaps ?.
as Kotlin has https://kotlinlang.org/docs/null-safety.html#safe-calls
{x: 1}?.y
would return null
instead of throwing an error
There are two jsonnet implementations which implement such feature:
Xtrasonnet - fork of sjsonnet, primarily focused to be embedded in Java apps: https://github.com/jam01/xtrasonnet/tree/0.5.4#what-kind-of-additions-to-the-jsonnet-language
Jrsonnet - general-purpose jsonnet implementation, has this feature as optional under the feature flag: https://github.com/CertainLach/jrsonnet/blob/master/docs/features.md#exp-null-coalescing
Hey @CertainLach yeah I'm the author behind xtrasonnet. Glad our syntax ended up being the same!
Oh, I haven't noticed this :D No, I have taken an idea from xtrasonnet, because often write such code, and this is fairly annoying.
The only thing my implementation has over yours is computed index syntax a?.[field]
, borrowed from javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#optional_chaining_with_expressions
Ah, great idea