New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow logical XOR using ^
operator
#587
Comments
Seems reasonable, though there may have been some reason for this I'm not aware of. |
Care will be required in resolving the type of the result. I suspect it should invalid to apply the operator to a |
Notes for design meeting
|
Discussed in design meeting and declined. Relevant points:
|
We went back and forth on this one. One of the confusing aspects of supporting a boolean ^ boolean is that we'd rather not support the same for & and |. The reasoning here is that this is a fairly common error to accidentally type & when you meant && (and likewise for | vs ||). We'd like to continue to help catch errors like this. This leaves ^ as an exception to the rule rather than a rule the other operators follow. For simplicity, I think the preference is just to instead prefer the pattern of using a !== b rather than a ^ b and introducing more type overloads for ^. |
Look at that, Ryan's message popped up. What he said :) |
Thanks for considering this. Given this decision, may I suggest improving the error message to state something akin to the following?
|
I like the improved error message. Logging an issue for that. |
@RyanCavanaugh !== is equivalent and not necessarily less clear (plus it returns a value of the appropriate type) I think the XOR operator is very important and can reduce some code, however why you can't make ^^ instead of single ^ |
@saideldah making new binary operators is TC39's job, not ours |
@RyanCavanaugh |
We don't want to add new expression-level syntax with runtime semantics. |
Consider:
Currently attempting to use the
^
operator onboolean
values fails with:The
|
and&
bitwise operators have logical counterparts,||
and&&
. No such^^
is available.JavaScript allows bitwise XOR to apply to
boolean
values.Currently workarounds are:
<number><any>a ^ <number><any>b
a !== b
The XOR operator is clearer than both of these.
The text was updated successfully, but these errors were encountered: