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

Suggestion: Overloading operator &= for boolean type #8424

Closed
klinki opened this issue May 2, 2016 · 8 comments
Closed

Suggestion: Overloading operator &= for boolean type #8424

klinki opened this issue May 2, 2016 · 8 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@klinki
Copy link

klinki commented May 2, 2016

It would be great to overload operator &= for booleans to produce boolean value as well. (If it is possible).

Right now in ES5 when you use

var boolean = true;
boolean &= true;

you get numeric value 1 instead of boolean true.

I noticed it because it produces compilation errors.
And since it is probably not possible in pure JS to fix that behaviour, it would be nice to compile it into expression which produces boolean as expected. For example:

var boolean = true;
boolean = boolean && true;

(The same would apply for operator |= )

@dsherret
Copy link
Contributor

dsherret commented May 2, 2016

See #5407.

@malibuzios
Copy link

malibuzios commented May 2, 2016

I'm not sure if #5407 is really related to this. This doesn't seem like it's really about providing user-defined 'operator overloading' but more like fixing an undesirable behavior of the run-time that may have to do with some sort of strange coercion that Javascript does.

The problem seems 'deeper' than this as in the JS runtime (including in strict mode);

var x = true;
var y = x & true; // y gets value 1;

However here TypeScript errors:

The & operator is not allowed for boolean types

Perhaps it could also error with &= as well when in this scenario, after all x &= true isn't really that different than x = x & true, which already errors?

@mhegazy
Copy link
Contributor

mhegazy commented May 2, 2016

I am not sure i understand the proposal. Currently both operators generate an error message. so:

var x = true;
x = x & true; // Error: The & operator is not allowed for boolean types. Consider using && instead
x &= true;  // Error: The & operator is not allowed for boolean types. Consider using && instead

See http://testsite-typescript-41eeb979-7eaa-4c74-9d47-9d182c7b61ab.azurewebsites.net/play/index.html#src=var%20x%20%3D%20true%3B%0D%0Ax%20%3D%20x%20%26%20true%3B%0D%0Ax%20%26%3D%20true%3B%20

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label May 2, 2016
@malibuzios
Copy link

You're right, I checked in the browser and noticed in worked but assumed based on the report it didn't error in TS, now I realize I misread it and forgot to actually check it.. :)

@malibuzios
Copy link

malibuzios commented May 2, 2016

I believe the & operator (the bitwise or) is not supposed to be used with booleans.

Perhaps the intention was to propose something like &&=?
And if that was available, I guess ||= would be sort of 'inevitable'?

I wonder if that was ever proposed to ES?

@dsherret
Copy link
Contributor

dsherret commented May 2, 2016

The original post is asking that this, which currently errors:

var b = true;
b &= true;

Be allowed and emitted to this:

var b = true;
b = b && true;

So that it would be a boolean; however, I referenced #5407 because it states:

One thing we avoid doing is type-driven emit. The idea is that TypeScript types have no bearing at runtime - the code is as close to the analogous JavaScript that you would have written. Given that, substituting in a method call for an operator would go against that.

I also disagree with the proposal because & is a bitwise operator and not a boolean operator and this proposal changes the behaviour of JavaScript—it would be a breaking change and not help people find problems in their code like this... though it may be intentional in some circumstances.

@malibuzios
Copy link

malibuzios commented May 2, 2016

I understand, however this is not always the case in other languages. In C# & is both the 'logical or' and 'bitwise or' and && is considered the 'short-circuiting logical or', so maybe that was the source of the confusion.

I can't see a technical reason not having &&= or ||= operators though. It could be suggested to ECMAScript, I guess (they did the add exponentiation (**) operator in ES7, I believe).

@mhegazy
Copy link
Contributor

mhegazy commented May 2, 2016

This would require type-directed emit. i.e. the operator will be emitted differently based on what the type system things its type is. TypeScript has avoided type-directed emit. this will also break our isolated module scenarios (i.e transpiling a single file in the browser without knowing the context). No type-directed emit has been a long standing axiom and serves to facilitate a fully-erasable type system, that has been one of the TS Design Goals

@mhegazy mhegazy closed this as completed May 2, 2016
@mhegazy mhegazy added Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript and removed Needs More Info The issue still hasn't been fully clarified labels May 2, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants