Repository navigation
Description
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:
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.
Activity
DanielRosenwasser commentedon Mar 22, 2019
Duplicate of #195.
orkhan-huseyn commentedon Sep 14, 2021
If you have red both PRs carefully they have different reasons. I was not proposing it as a syntactic sugar.