lukescott / es-type-hinting

Typing hinting syntax for ECMAScript
3 stars 2 forks source link

Typed Objects #31

Open Naddiseo opened 9 years ago

Naddiseo commented 9 years ago

How will we interact with: https://github.com/dslomov-chromium/typed-objects-es7 This also plays into #23

lukescott commented 9 years ago

I think group types (uint8, int8, uint16, int16, uint32, int32, float32, float64) are like the existing types number, string, and boolean (passed by value). They should have defaults according to their type. Anything that is always passed by reference, such as an Array/Object, should default to undefined.

Should probably be a note on how we would handle it. Probably shouldn't be in the syntax yet.

Naddiseo commented 9 years ago

Well, also how does it work with:

let PointType = StructType({
    x: uint8,  // Our current syntax would allow `x number: uint8`
    y: uint8
});

let p PointType; // does this default to undefined or to PointType({x: 0, y: 0}) ?
Naddiseo commented 9 years ago

I think we have some overlap with https://github.com/dslomov-chromium/typed-objects-es7#convertandcopytotypedescriptor-dimensions-buffer-offset-value

lukescott commented 9 years ago

I think they are assuming no type hinting exists and uint8 are values themselves representing a type. That would likely change to:

let PointType = StructType({
    x uint8,
    y uint8
});

or even:

let PointType = {
    x uint8,
    y uint8
};

Does StructType get passed by value?

Naddiseo commented 9 years ago

They're treating all types as first class objects/functions, whereas we're treating them as "hints". We might need to think about that, but it does look like this proposal still works with theirs given your examples. If it is changed to what you suggest, then we'll have to write into our semantics that the type of something is stored:

Static Semantics:
PropertyDefinition : IdentifierReference TypeHint

1. Assert Type(TypeHint) is not undefined
2. Set the [[TypeHint]] internal slot of IdentifierReference to TypeHint
lukescott commented 9 years ago

Given that this is already stage 1, I would take this proposal seriously.

Naddiseo commented 9 years ago

From #32:

Does typed objects in #31 have a symbol type that we can use? It has Type Objects. Would that qualify?

The type objects get defined in terms of [[structure]]s, which I think we could piggyback onto for base types, and would possibly work with reflection as well. I'm still not sure how it would work with forward references:


function getA() A { // In temporal deadzone...
    return new A();
}

let A = new StructType();

// desugared:
function getA() { return new A(); }
%Set(getA, [[TypeDescriptor]], A); // temporal deadzone...

let A = new StructType();

I guess that's more of something for a transpiler/engine, so that type descriptors of functions get set at the end of the file, or after everything they reference. I'm not sure.

Naddiseo commented 9 years ago

More information: https://github.com/nikomatsakis/typed-objects-explainer

Also solves the forward referencing problem: https://github.com/nikomatsakis/typed-objects-explainer/blob/master/ootypes.md#cyclic-type-definitions

Naddiseo commented 9 years ago

Also this: https://github.com/nikomatsakis/typed-objects-explainer/blob/master/classsyntax.md