Operator Mapping Library Extensions#969
Merged
Perryvw merged 12 commits intoTypeScriptToLua:masterfrom Jan 26, 2021
Merged
Conversation
lolleko
reviewed
Jan 25, 2021
Perryvw
reviewed
Jan 25, 2021
language-extensions/index.d.ts
Outdated
| declare function $multi<T extends any[]>(...values: T): MultiReturn<T>; | ||
| declare type MultiReturn<T extends any[]> = T & { readonly " __multiBrand": unique symbol }; | ||
|
|
||
| declare type LuaAdd<TA, TB, TR> = ((a: TA, b: TB) => TR) & { readonly __luaAddBrand: unique symbol }; |
Member
There was a problem hiding this comment.
We should probably add jsdoc comments explaining these language extensions (we should also for multi & multireturn, but can be in another PR) so that people that didn't write but just consume declarations can also understand what is happening. Something like:
/**
* Calls to functions with this type are translated to `a + b`.
* For more information see: https://typescripttolua.github.io/docs/advanced/language-extensions
*
* @param TA The type of the left-hand-side of the operation.
* @param TB The type of the right-hand-side of the operation.
* @param TR The resulting (return) type of the operation.
*/
declare type LuaAdd<TA, TB, TR> = ((a: TA, b: TB) => TR) & { readonly __luaAddBrand: unique symbol };ALSO: Thoughts on renaming the type params to TLeft, TRight, TReturn?
| `; | ||
|
|
||
| const binaryMathOperatorTests: Array<[string, number, number, number]> = [ | ||
| ["LuaAdd", 7, 4, 11], |
Member
There was a problem hiding this comment.
It's not obvious what this means at first, might be more readable if you use objects instead of tuples, ie { extensionType: "LuaAdd", left: 7, right: 4, expectedResult: 11 }, similar to https://github.com/TypeScriptToLua/TypeScriptToLua/blob/master/test/unit/assignments.spec.ts#L408
Co-authored-by: Perry van Wesel <Perryvw@users.noreply.github.com>
Perryvw
approved these changes
Jan 26, 2021
lolleko
approved these changes
Jan 26, 2021
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #964
Support for mapping lua's metamethod-based operator overloads to function declarations. This allows operator overloads provided by libraries to be callable from TS.
A common example of this would be a math vector class:
To solve this, declaration writers can now map the add operator to a function:
Mappings don't need to be global functions:
They can also declared as methods on the object using an alternate type:
Overloads are allowed using intersections and work as expected:
Currently, the following operators are supported: