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

Proposal: syntax sugar for binary compare expressions #2903

Closed
tinganho opened this issue Apr 24, 2015 · 6 comments
Closed

Proposal: syntax sugar for binary compare expressions #2903

tinganho opened this issue Apr 24, 2015 · 6 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@tinganho
Copy link
Contributor

Auto referring to last identifier:

Instead of:

if(value > 2 && value !== 3) {}

we can have this:

if(value > 2 && !== 3) {}

Auto referring to last identifier and operator:

Instead of:

if(value === 2 && value === 3) {}

we can have this:

if(value === 2 && 3) {}

Range comparisons:

for numbers:

if(value === 1..3) {}

for enums:

if(value === Enum.something...Enum.somethingElse) {}
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Apr 24, 2015
@DanielRosenwasser
Copy link
Member

Hey @tinganho, thanks for the suggestions, however, you'll have to understand that we tend to be somewhat conservative when it comes to syntax. Additionally, I'm not sure that these fall in line with the direction of our language.

if(value === 2 && 3) {}

Definitely not something we could do, this is already syntactically valid, and part of common patterns.

if(value === 1..3) {}
if(value === Enum.something...Enum.somethingElse) {}

We'd be reinventing new semantics for ===. Even worse, if a range operator ever made its way into ECMAScript, the expected semantics would probably not match this.

if(value > 2 && !== 3) {}

I think this syntax this strays too far from the general direction we want to move in, as well as the direction of JavaScript itself.

@tinganho
Copy link
Contributor Author

@DanielRosenwasser I missed the valid syntax part. Though these are problems I currently have today(typing the same identifier too many times).

@cyberpunk2099
Copy link

I think you may consider 'in' operator for range checks, like:

if(value in 1..3)

or

if(value in Enum.something..Enum.somethingElse)

or even

if(value in Enum.something..Enum.somethingElse, Enum.somethingSpecial)

So that you won't have to reinvent new semantics for ===

@tinganho
Copy link
Contributor Author

I think I also have seen this syntax somewhere: I think Swift.

if(object.property?.subProperty) {}

will be the same as:

if(object.property && object.property.subProperty) {}

@RyanCavanaugh
Copy link
Member

These are all suggestions that are on the table for ES7 or later, and thus need to be avoided by TypeScript until their semantics are fully understood.

For example, the safe navigation operator can't be simply decomposed as written because there are values which are safe to dot off of that are still falsy:

var x = { n: "" };
var y = x?.n?.length; // y should be 0, not undefined or ""

@DanielRosenwasser
Copy link
Member

Also, any discussion on that feature should not be continued here, as it's tracked under #16.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 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