Skip to content

Distinguishing types between int and double #30543

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

Closed
5 tasks done
orkhan-huseyn opened this issue Mar 22, 2019 · 2 comments
Closed
5 tasks done

Distinguishing types between int and double #30543

orkhan-huseyn opened this issue Mar 22, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@orkhan-huseyn
Copy link

orkhan-huseyn commented Mar 22, 2019

Search Terms

int double, integer, float, double

Suggestion

This proposal introduces new number types int and double or float. Goal is to make a difference between integers and floating point numbers since some JavaScript engines does such kind of difference in the back-end.

Use Cases

As we know about V8 engine, it uses pointer tagging to distinguish between different types such as Smi (small integer), HeapObject, WeakHeapObject etc.

Consider this example:

const o1 = {};
o1.x = 3;
o1.y = 4;

const o2 = {};
o2.x = 5;
o2.y = 6;

For both o1 and o2 object the engine will create same shapes. See image below:

However, after changing o2.x to some double value, say that o2.x = 5.5; then V8 will use create new subtree from that shape and the old subtree in this shape will be marked as deprecated and will be garbage-collected. Reason for this is because there is a difference how V8 stores Smi and Double values in the memory.

Here is second image after changing x to 5.5:

Read more here: https://ponyfoo.com/articles/javascript-performance-pitfalls-v8#smi-to-double-migrations

Examples

Some code examples from the proposal:

class Point {
   constructor(private x: int, private y: int) {}
   
   getX(): int { this.x = x; }
   getY(): int { this.y = y; }
 }

let point = new Point(12, 5);
point.x = 1.5; // Error: double is not assignable to integer

This proposal does not require a breaking change, since number types are also okay, if one considers some possible performance pitfalls.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Mar 22, 2019
@DanielRosenwasser
Copy link
Member

Duplicate of #195.

@orkhan-huseyn
Copy link
Author

Duplicate of #195.

If you have red both PRs carefully they have different reasons. I was not proposing it as a syntactic sugar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants