sirisian / ecmascript-types

ECMAScript Optional Static Typing Proposal http://sirisian.github.io/ecmascript-types/
453 stars 4 forks source link

Generator overloading support #39

Closed sirisian closed 5 years ago

sirisian commented 6 years ago

Been meaning to write this for a while, but take this as very WIP and ad-hoc. So with function overloading is generator overloading. In most cases Object.entries, Object.keys, and Object.values are no longer needed with this support.

A basic iterable:

var o = {};
o[Symbol.iterator] = function* ()
{
    yield* [1, 2, 3];
};
[...o]; // [1, 2, 3]

Overloaded example:

var o = {};
o[Symbol.iterator] =
[
    function* ():int32
    {
        yield* [1, 2, 3];
    },
    function* ():[int32, int32]
    {
        yield* [[0, 1], [1, 2], [2, 3]];
    }
];

[...o:int32]; // [1, 2, 3] Explicit selection of the generator return signature
for (const a:int32 of o) {} // Type is optional in this case
[...o:[int32, int32]]; // [[0, 1], [1, 2], [2, 3]]
for (const [a:int32, b:int32] of o) {} // Type is optional in this case
sirisian commented 6 years ago

https://github.com/sirisian/ecmascript-types#generator-overloading

Added the section since the destructuring syntax was added for return types.