Skip to content
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

TorqueScript anonymous functions #774

Closed
crabmusket opened this issue Sep 5, 2014 · 2 comments
Closed

TorqueScript anonymous functions #774

crabmusket opened this issue Sep 5, 2014 · 2 comments
Milestone

Comments

@crabmusket
Copy link
Contributor

As in this resource and popular other languages. Anonymous functions are great. However, we should probably decide on an identifier for them which is not function, so as to not treat every function as a lambda, but also because JavaScript has shown us that typing out function everywhere is annoying. Additional enhancement would be to allow anonymous function bodies to have no curly braces around them, so we can write very nice short function bodies.

I propose using a simple backslash as the lambda introducer; to whit:

// Using a traditional block;
schedule(1000, 0, \(%name) {
   echo("Hello" SPC %name);
}, "Peter");

Additionally, we should support calling functions from variables:

%sch = "schedule";
%sch(1000, MyObject, myMethod);

%fn = \(%x) %x + 1;
%y = %fn(5);
@crabmusket crabmusket added this to the 3.7 milestone Sep 5, 2014
@JeffProgrammer
Copy link
Contributor

Here's the Torque3D compatible code from that resource found in these 2 commits on my game:

https://github.com/JeffProgrammer/BeyondReality/commit/10ee5c799a1bfa721fd9291553197966a2d51c16

https://github.com/JeffProgrammer/BeyondReality/commit/f492e44b0362eef583f2920dad0aaa2a094e0c49

this will allow you to do the following:

schedule(1000, 0, lambda(%name) {
echo(%name);
}, "Jeff");

@crabmusket
Copy link
Contributor Author

Working branch. Decided to go with plain old function(). Currently the function name is returned, but not defined until the end of the current script parsing. This means that anon functions can't be used in top-level expressions where they are called immediately - for example, typing in the console call(function(%a) { echo(%a); }, horses);. Fixed in 170a4ea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants