X Tutup
Skip to content

New functions - mixed context overloads#291

Merged
Perryvw merged 25 commits intoTypeScriptToLua:new-functionsfrom
tomblind:new-functions
Dec 2, 2018
Merged

New functions - mixed context overloads#291
Perryvw merged 25 commits intoTypeScriptToLua:new-functionsfrom
tomblind:new-functions

Conversation

@tomblind
Copy link
Collaborator

This update addresses issues with function overloads that mix signatures with and without context:

declare class Foo {
    method(s: string): string; // Has context
    method(this: void, s: string, n: number): string; // No context
}

Now, calling the overload will correctly use dot or colon based on the resolved signature:

declare const foo: Foo;
foo.method("foo");
foo.method("bar", 17);
foo:method("foo"); -- Uses colon
foo.method("bar", 17); -- Uses dot

Additionally, assignments to and from function types that have mixed overloads are now banned. Properly validating these assignments would require manually signature matching the overloads and would add a lot of complexity for a fairly fringe-y case. The error advises using only overloads that are all functions or all methods, but not both.

interface Func { (s: string, n: number): string };
let fn: Func = foo.method; // Error: Unsupported assignment of mixed function/method overload. All overloads should either be functions or methods.

src/Errors.ts Outdated
node);
} else {
return new TranspileError(`Unsupported assignment of mixed function/method overload. `
+ `All overloads should either be functions or methods.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error is a little missleading, how about rephrasing to 'Overloads should either all be methods, or all be functions.'

&& !(ts.getCombinedModifierFlags(sigDecl.parent) & ts.ModifierFlags.Static)) {
// Non-static lambda property
return true;
return ContextType.NonVoid;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought lambda properties are currently transpiled without context, would that not mean void type here, or do we want to change this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lambda properties get a dummy context (versus stand-alone lambdas, which do not). This ensures they can be called the same as methods when obfuscated behind an interface.

const type = this.checker.getTypeAtLocation(node.expression);
const op = tsHelper.isFunctionWithContext(type, this.checker) ? ":" : ".";
const sigDecl = sig.getDeclaration();
const op = !sigDecl || tsHelper.getDeclarationContextType(sigDecl, this.checker) !== ContextType.Void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try not to abbreviate variables too much, I don't think there is a lot of abbreviations on other variable names, we should probably try to be somewhat consistent.

@tomblind
Copy link
Collaborator Author

tomblind commented Dec 2, 2018

Variable names and the error message are updated. Sorry I was getting lazy there 😄

@Perryvw Perryvw merged commit 281ab54 into TypeScriptToLua:new-functions Dec 2, 2018
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.

2 participants

X Tutup