Skip to content

@luaIterator changed to apply to type, not function#448

Merged
tomblind merged 1 commit intomasterfrom
lua-iterator-v2
Feb 24, 2019
Merged

@luaIterator changed to apply to type, not function#448
tomblind merged 1 commit intomasterfrom
lua-iterator-v2

Conversation

@tomblind
Copy link
Copy Markdown
Collaborator

fixes #396

Currently, the @luaIterator directive is applied to a function that generates an iterator. This causes issues with tracking that a type is a lua iterator for use in for...of loops.

declare namespace string {
    /** @luaIterator @tupleReturn */
    export function gmatch(s: string, pattern: string): Iterable<string[]>;
}

const iter = string.gmatch("foobar", ".");
for (const [c] of iter) {} // 'iter' is seen as a regular Iterable and won't transpile correctly here

This PR changes the @luaIterator so that it is applied to the type itself:

declare namespace string {
    /** @luaIterator @tupleReturn */
    export interface GmatchResult extends Iterable<string[]> {}
    export function gmatch(s: string, pattern: string): GmatchResult;
}

This allows that iterator to be passed around freely without losing the directive. It will correctly transpile to a native for...in loop regardless of its source.

Note this is a breaking change! But the old way was error-prone and inconsistent with how other directives work, so I believe it's worth requiring declarations to be updated.

@tomblind tomblind merged commit bbb4813 into master Feb 24, 2019
@tomblind tomblind deleted the lua-iterator-v2 branch February 24, 2019 14:51
hazzard993 pushed a commit to hazzard993/TypescriptToLua that referenced this pull request Feb 26, 2019
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.

Passing Iterators

2 participants