Skip to content
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

Closed
drewnoakes opened this issue Sep 3, 2014 · 12 comments
Closed

Allow logical XOR using ^ operator #587

drewnoakes opened this issue Sep 3, 2014 · 12 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@drewnoakes
Copy link
Member

Consider:

var a: boolean = ...;
var b: boolean = ...;

if (a ^ b) {
  // ...
}

Currently attempting to use the ^ operator on boolean values fails with:

error TS2113: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

The | and & bitwise operators have logical counterparts, || and &&. No such ^^ is available.

JavaScript allows bitwise XOR to apply to boolean values.

Currently workarounds are:

  • casting: <number><any>a ^ <number><any>b
  • using a negated equality test: a !== b

The XOR operator is clearer than both of these.

@RyanCavanaugh
Copy link
Member

Seems reasonable, though there may have been some reason for this I'm not aware of.

@drewnoakes
Copy link
Member Author

Care will be required in resolving the type of the result. I suspect it should invalid to apply the operator to a number and a boolean.

@RyanCavanaugh
Copy link
Member

Notes for design meeting

  • Runtime behavior
    • Operands of ^ are converted to number
    • The result is always number
    • e.g. true ^ false is 1, false ^ false is 0, true ^ 1 is 3 (truthy)
    • Shortest actual workaround for boolean-typed x ^ y is +x ^ +y
  • Recommendations
    • The result of the operation is of type number
    • number ^ boolean or boolean ^ number is disallowed

@sophiajt sophiajt added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Sep 19, 2014
@sophiajt sophiajt added this to the Community milestone Sep 19, 2014
@sophiajt sophiajt added Declined The issue was declined as something which matches the TypeScript vision and removed Help Wanted You can do this labels Sep 19, 2014
@RyanCavanaugh
Copy link
Member

Discussed in design meeting and declined. Relevant points:

  • !== is equivalent and not necessarily less clear (plus it returns a value of the appropriate type)
  • If we supported ^ for boolean, we'd want to support & and | for consistency, but catching && vs & errors is important

@RyanCavanaugh RyanCavanaugh removed this from the Community milestone Sep 19, 2014
@sophiajt
Copy link
Contributor

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 ^.

@sophiajt
Copy link
Contributor

Look at that, Ryan's message popped up. What he said :)

@drewnoakes
Copy link
Member Author

Thanks for considering this.

Given this decision, may I suggest improving the error message to state something akin to the following?

error TS0000: The ^ operator is not allowed for boolean types. Consider using !== instead.

@sophiajt
Copy link
Contributor

I like the improved error message. Logging an issue for that.

@saideldah
Copy link

@RyanCavanaugh
"Discussed in the design meeting and declined. Relevant points:

!== is equivalent and not necessarily less clear (plus it returns a value of the appropriate type)
If we supported ^ for boolean, we'd want to support & and | for consistency, but catching && vs & errors is important"

I think the XOR operator is very important and can reduce some code, however why you can't make ^^ instead of single ^

@RyanCavanaugh
Copy link
Member

@saideldah making new binary operators is TC39's job, not ours

@saideldah
Copy link

@RyanCavanaugh
Ok, thanks for the clarification.
what you are trying to say that you can't add something that does not already exist in Javascript.
is that right?

@RyanCavanaugh
Copy link
Member

We don't want to add new expression-level syntax with runtime semantics.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants