Can we get !in
for expressions?
e.g. if ("Notification" !in window) {}
instead of if (!("Notification" in window)) {}
Can we get !in
for expressions?
e.g. if ("Notification" !in window) {}
instead of if (!("Notification" in window)) {}
If we did, I'd prefer key not in object
rather than making a syntax soup with !
.
It'd also have to work on instanceof
.
That'd work for me.
variable! in object
also is already valid TypeScript syntax (a non-null assertion). not in
reads nicer and has better compatibility.
See also Not keyword operators
i'm sort of surprised to see people prefer not in
since not
is not otherwise a JS operator. like, it's very nice in python but not
is python's normal logical negation. i'd think !in
(and !instanceof
for that matter) is perfectly clear and doesn't feel like random symbols being thrown around. since !
is JS's not
, it's just the obvious JS equivalent of not in
, IMO.
! negates a value, not an operator.
sure but you could say the same for python's not
, right? clearly this is not just a normal use of !
(otherwise it would already work). it's new syntax but its meaning seems clear in light of the existing meanings for !
and in
.
I don't know anything about Python :-) From a purely JS perspective, when I see !
, I think negation of a value - or, in TS, i might also think it's a non-null assertion.
I do see @peet's point. Yes, today ! only negates values, but I don't see that as a strong argument in and of itself for not having "!in" syntax.
I've been sitting on the fence between "!in" and "not in" but as I think about it more, I'm leaning more and more towards "not in", the reason being that syntax soup argument given earlier. Yes, ! reads as "not" so it does make some sense to use it in this context, but the way it's getting used requires us giving "!" different behaviors depending on how it's used, which adds unnecessary confusion. E.g. say I know that "!" exists and what it does, but I hadn't yet learned that the language provides negates forms of different operators. If I read code that says "x !in y", how should I interpret that to google it? Maybe there's a single operand version of the in operator, so that's actually "x (!(in y))"? Even if you do know that "!in" is a thing, I feel like it still adds a (small) extra cognitive overhead to mentally parse it.
So yes, I feel like "not in" is better, and even better (though uglier) would be something like "not.in" or "notIn", making it conceptually a single operator as opposed to an operator with a modifier.
So if I were redesigning python today, I would "notIn" instead of "not in" as the operator.
Let's consider these four proposals:
// (A)
if ('x' !in center) {}
if (center !instanceof Coord) {}
// (B)
if ('x' not in center) {}
if (center not instanceof Coord) {}
// (C)
if ('x' notIn center) {}
if (center notInstanceof Coord) {}
// (D)
if ('x' notin center) {}
if (center notinstanceof Coord) {}
(B)
make no sens: Why is there a space between not
and instance
but not between instance
and of
?(C)
, why would the camel case be partial?(D)
, I find notinstanceof
quite hard to read (but why not)So (A)
seems to me to be the best choice.
A proposal (stage 1) has been created for this. It's suggesting the use of !in
and !instanceof
because instanceof
is already a thing that exists. You can do ! instanceof
and ask the same question.
what about !=
or !==
?
True, those are indeed operators negated by a !. However, they're not keywords.
It makes sense to me that a token operator can be negated by a ! token, but a word operator needs to be negated by a word.
Probably worth noting, pattern matching introduces a not
keyword for negating