Open bbrk24 opened 8 months ago
So if I understand correctly, the proposal is arr?.# ?+ 1 ?? 0
. Looks nice to me! I imagine it could work for custom operators, instanceof
, in
, etc.
A couple notes:
?=
is the exact opposite of its current behavior.x?++
is reminiscent of optional index access / calls, but ?-x
looks a little strange.
Related: #897
Here's a situation I've run into before: given an optional array, return one more than its length if it's defined, and some fallback value (e.g. 0) if it isn't. Currently this can be done in two ways:
The first one is a little verbose, especially if it's embedded in a longer expression. The second one is less obvious why it works, and doesn't easily accommodate a configurable fallback value.
C#'s mathematical operators are optional-chained by default, so this can be written as
While we can't change the behavior of the existing JS operators -- after all,
null + ''
is still meaningful -- it would be nice for there to be a syntax for this, e.g.?+
.