Skip to content

Spread arguments #238

Closed
Closed
@RyanCavanaugh

Description

@RyanCavanaugh

Example

f(1, 2, ...items, 3, ...more)

Codegen

f(...items); // ... becomes...
f.apply(void 0, items);  // strict mode.  Otherwise, 'this'

obj.foo(...items); // ... becomes...
obj.foo.apply(obj, items); // , but only want to evaluate obj once, so we inject an:
function __apply(obj, method, args) {
    obj[method].apply(obj, args);
}
__apply(obj, "foo", items);

CoffeeScript introduces a fresh var, hoists it to top, and uses it to prevent re-eval of 'obj'. Local vars could be done inline

Typecheck

f(1, 2, ...items, 3, ...more) : f ( T, T, BCT (...items, 3, ...more));

Issue is that this would prevent potentially valid code from passing typecheck.  Key idea is that we would be checking the arity that we know as well as the BCT, knowing that the spread arrays could be empty.

Questions

Do we want to be able to spread array-like, like the array-like objects that come from the DOM? If we do, we could use the CoffeeScript approach. We could tell arrays and array-like apart statically in the type system.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CommittedThe team has roadmapped this issueES6Relates to the ES6 SpecSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions