-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add lambdas to E2 #2829
Add lambdas to E2 #2829
Conversation
* Functions are now tables, containing parameter signature, return type and inner function * function:getParameterTypes() * function:getReturnType() * Don't reset global variables on `@strict`, fixing issue with top level locals not working as upvalues since they'd be reset by the runtime. I don't think this would actually affect anyone since you shouldn't be able to use variables before they're assigned, but it's `@strict` only behavior anyway.
Note timers and other libraries that'd make use of lambdas are not implemented in this PR. They will come in separate PRs. Implementations should be fairly small/easy |
Only problem right now is that the editor does not like the syntax and highlights a lot of things incorrectly |
I'd already fixed this bug with functions that have return values but this was not fixed for functions without return values. Also added a test case for this.
local SayMessage = function() {}
const SetMessage = function(Message:string) {
SayMessage = function() {
return Message
}
}
SetMessage("There's a snake in my boot!")
assert( SayMessage()[string] == "There's a snake in my boot!" )
assert( SayMessage()[string] == "There's a snake in my boot!" )
SetMessage("Reach for the sky!")
assert( SayMessage()[string] == "Reach for the sky!" ) |
* Add tests to ensure variadic parameters, void parameters and implicit parameters aren't allowed * Fix lambdas potentially not returning at all codepaths like functions now are expected to do. Added a test for that.
Could it be possible to get references to E2functions (without wrapping them using E2)? Probably too much for this PR but I wanted to have references to the easing functions. |
Can be done later, maybe alongside lowercase variables |
I should stop prematurely optimizing this.. This is like the third or second time I've made this mistake lol
Actually a really easy fix. Surprising
Lambdas are anonymous functions that can be used as expressions, and have upvalues.
They are type erased but store their types at runtime, similar to tables.
These are the second part of the transition away from the trigger system.
Fixes #1677
Syntax
They're TS-style ES5 anonymous functions.
Arrow functions could potentially be added in the future.
Nicest thing about this syntax is there's no explicit return type needed, compiler can see the return type itself. Could bring this functionality to regular functions in the future.
Op costs
Costs 15 ops to call, 25 to create.
How they work
The implementation is pretty simple, just stores the current scopes whenever the lambda is made, and set the current scopes to that when it is called.
Caveats
Variadic parameters currently aren't allowed and won't be for the foreseeable future, as to not slow down calls. Maybe this could be done in the future but I don't see it as worth it right now
This will give you a compile error
Developer Representation
They are stored as a table with a metatable.
Here's a basic coroutine example
And the emmylua annotations
Other changes
On
@strict
global variables won't be reset, this is to fix this code not working properlyI don't think this should affect people since you really shouldn't have been able to use variables before they are set, and the alternative would be a large change to how variables are stored which I don't think is worth it. Either way it's only for
@strict
.String calls
String calls have been deprecated, and will soon be an error on
@strict
.String calls now cost 25 ops to call, I might have missed this in the compiler rewrite, not entirely sure.