Skip to content

@varArg directive#630

Merged
tomblind merged 10 commits intomasterfrom
feature/elipsis-forward
Jun 13, 2019
Merged

@varArg directive#630
tomblind merged 10 commits intomasterfrom
feature/elipsis-forward

Conversation

@tomblind
Copy link
Copy Markdown
Collaborator

When a rest parameter is referenced with a spread operator, the transformed code packs and unpacks it:

function foo(this: void, ...args: unknown[]) {
    console.log(...args);
}

=>

function foo(...)
    local args = ({...})
    print(unpack(args))
end

This PR adds a new directive @varArg to allow direct access to lua vararg operator .... This can be applied to an array-like type and any references to an identifier with that type will be directly transformed into ...:

/** @varArg */
interface LuaVarArg<T> extends Array<T> {}

function foo(this: void, ...args: LuaVarArg<unknown>) {
    console.log(...args);
}

=>

function foo(...)
    print(...)
end

Aside from function varargs, this can be used to access the file-scope varargs:

declare const arg: LuaVarArg<string>;
console.log(...arg);

=>

print(...)

To support tuple rest arguments, the vararg type must be declared like this:

/** @varArg */
type LuaVarArg<A extends unknown[]> = A & { __luaVarArg?: never };

function foo(this: void, ...args: LuaVarArg<[string, number]>) {
    console.log(...args);
}
foo("A", 2);

Note that this PR also adds stripping of the rest parameter variable (local args = ({...})) if it is not needed.

@ark120202
Copy link
Copy Markdown
Contributor

I think it would be better to call it @vararg, without considering them separate words. That's how it's written in Lua Reference Manual.

@tomblind
Copy link
Copy Markdown
Collaborator Author

I think it would be better to call it @vararg, without considering them separate words. That's how it's written in Lua Reference Manual.

Sure. The directives aren't case-sensitive, so it's just a matter of what we put in the wiki when documenting it.

@tomblind tomblind requested a review from Perryvw June 12, 2019 22:57
@tomblind tomblind merged commit acd036b into master Jun 13, 2019
@tomblind tomblind deleted the feature/elipsis-forward branch June 13, 2019 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants