Skip to content

Primitive type implementation #875

Closed
@wangyanxing

Description

@wangyanxing

Primitive Type Implementation

We have been working on adding support for primitive types in TypeScript as part of a research project at the San Francisco State University. This issue documents our efforts and we would like to solicit feedback from the community. The following fork of the TypeScript repo contains a complete reference implementation of our proposal:

https://github.com/wangyanxing/TypeScript

The following Google Doc contains a formal specification of the syntax and semantics of primitive types in TypeScript:

https://docs.google.com/a/puder.org/document/d/120hs8AJ0-WGwHbvV7D3XOJ2b18ZceBvYjOlDcR1Dn_A/edit?usp=sharing

We have activated the commenting feature for the Google Doc. The following gives a quick overview of the changes we have made to the TypeScript language.

There are ten new primitive types in our implementation:
int, uint, i8, u8, i16, u16, i32, u32, float, double.

The value ranges of these types are documented in the Google Doc.

Here are some examples:

Variables and Arithmetic

The following code:

var var1 : u8 = 256;
var var2 : i8 = 200;
var var3 : u16 = 600000;
var var4 : i16 = 100000;
var var5 : int = 3.14;
var var6 : uint = var3 * 10000;
var var7 : double = 0.123456789;
var var8 : float = var7;
var var9 : u8[] = [var4,var5,var6];
var var10 : int = 3 / 4;
var var11 : int = 1 + 2;
var var12 : int = var5 + 2;

would emit:

var var1 = 256 & 255;
var var2 = (200 & 255) << 24 >> 24;
var var3 = 600000 & 65535;
var var4 = (100000 & 65535) << 16 >> 16;
var var5 = 3.14 | 0;
var var6 = var3 * 10000 >>> 0;
var var7 = 0.123456789;
var var8 = Math.fround(var7);
var var9 = [var4 & 255, var5 & 255, var6 & 255];
var var10 = 3 / 4 | 0;
var var11 = 1 + 2 | 0;
var var12 = var5 + 2 | 0;

Note: some platforms like node.js doesn’t support Math.fround() function. Our version of the TypeScript compiler would emit a compatibility version of that function in the generated code.

Functions

The following code:

function fun(x : i32) : u8 {
    x++;
    return x;
}
fun(255.9); // the function will return 0

would emit:

function fun(x) {
    x = x | 0;
    var _$0;
    (_$0 = x, x = x + 1 | 0, _$0);
    return x & 255;
}
fun(255.9);

The following code:

var func1 = (x:int) : u8 => { return x * 2; }
var func2 =function(x:int, y:int): u8 { return x+y };
func1(200.4);
func2(255.7, 1.9);

would emit:

var func1 = function (x) {
    x = x | 0;
    return x * 2 & 255;
};
var func2 = function (x, y) {
    y = y | 0;
    x = x | 0;
    return x + y & 255;
};
func1(200.4);
func2(255.7, 1.9);

The following code:

function fun_opt_arg(x? : int) : int {
    return x;
}

would raise an error since we forbid primitive type variables to be optional arguments:

error TS8001: Primitive type declaration 'x' cannot be optional.

Generics

The following code:

class GenericNumber<T> {
    add: (x: T, y: T) => T;
}

var geni8 = new GenericNumber<i8>();
geni8.add = function(x:i8, y:i8):i8 { return x + y; };

var genu8 = new GenericNumber<u8>();
genu8.add = function(x:u8, y:u8):u8 { return x + y; };

geni8.add(1.9, 2.9); // it will return 3
genu8.add(255, 1); // it will return 0

would emit:

var GenericNumber = (function () {
    function GenericNumber() {
    }
    return GenericNumber;
})();
var geni8 = new GenericNumber();
geni8.add = function (x, y) {
    y = (y & 255) << 24 >> 24;
    x = (x & 255) << 24 >> 24;
    return (x + y & 255) << 24 >> 24;
};
var genu8 = new GenericNumber();
genu8.add = function (x, y) {
    y = y & 255;
    x = x & 255;
    return x + y & 255;
};
geni8.add(1.9, 2.9); // it will return 3
genu8.add(255, 1); // it will return 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    DiscussionIssues which may not have code impact

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions