-
Notifications
You must be signed in to change notification settings - Fork 23
Symmetric operator? #4
Comments
That code seems to be using short-circuiting, and not the return value - can you elaborate more on the use case of it, besides simply being a negated form? |
@ljharb Yes the negated form is just drop the non-null value, that's why I'm not sure whether it's useful. I just list it here for others can check the idea :-) |
|
@tvald can you provide some more concrete code (without non-JavaScript notation) that explains this use case? If we can all only think of "there might be a use case" then that means there probably isn't yet a use case we need to worry about. |
NB: I'm not advocating for the implementation of a nullish-guard operator. I do think understanding what nullish-guard is clarifies the space in which optional-chaining and nullish-coalesce exist. (And helps future-proof them.)
Just as It just so happens that the most common usage of the guard pattern is for optional-chaining, but there are other cases where it can crop up. |
When you want to use a “nullish-guard”, you can just write: a != null && b or, if you insist to get another value than a == null ? undefined : b That doesn’t seem to mandate yet another operator. An important difference with nullish-coalescing: a ?? b
a != null ? a : b and optional chaining: a?.b
a == null ? undefined : a.b is that, in absence of such operators, you need either to repeat the expression |
I made the point in #61 and I think it's valid here: a lot of people consider loose equality operators ( I would argue that if we're going to have an OR-like nullish operator, it only makes sense to pair it with an AND-like operator. The point in both cases is to best convey intent, right? |
Closing, since this proposal is at stage 4. |
??
op:a ?? b
=>a != null ? a : b
We may also introduce a symmetric operator:
?!
op:a ?! b
=>a == null ? a : b
Sometimes I saw code like that:
a && f(a)
,f1() && f2() && f3()
which seems fit?!
op. But I'm not sure whether it is useful enough.The text was updated successfully, but these errors were encountered: