Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Future Improvements (https://forums.graalonline.com/forums/forumdisplay.php?f=10)
-   -   Function Closures! (https://forums.graalonline.com/forums/showthread.php?t=134262855)

WhiteDragon 04-17-2011 04:01 AM

Function Closures!
 
I've requested this once before, but I really think this should happen. Currently we have anonymous functions, but they are a slight pain to use, because you can't use variables defined outside of the function without explicitly passing them in.

Function closures are when the environment outside of the anon functions are "closed" inside of them, letting you use the variables defined outside of the anon functions inside of them.


Extremely simple example:
PHP Code:

function foo() {
  
temp.3;
  
temp.= function (temp.x) {
    return 
temp.temp.a;
  }
  
temp.f(4); // would ideally return 12


In temp.f, you should be able to access the temp variables outside the anonymous function.

Note: You can set this variables to pass things in, but then you need to handle all the garbage collection yourself because the anonymous function can still exist after the surrounding function ends (i.e., by returning it). This is basically impossible since you don't know when the function is destroyed. It's also really messy and ugly.



An example where this would be useful:
PHP Code:


// takes a function and list, and returns a new list with that function applied to every element
function map(temp.ftemp.list) {
  
temp.newList = {};
  for (
temp.0temp.temp.list.size(); temp.i++) {
    
temp.newList.insert(temp.itemp.f(temp.list[temp.i]));
  }
  
  return 
temp.newList;
}

function 
onPlayerChats() {
  
temp.int(player.chat);
  
temp.= function (temp.x) {
    return 
temp.x*temp.a;
  };
  
temp.list = {1,2,3,4};
  echo(
this.map(temp.ftemp.list));


This map function can already be defined. It is temp.f that can't be defined yet.

When you chat "10", that should echo out "10,20,30,40".

WhiteDragon 06-09-2011 03:19 AM

It would be neat if we could get this, so GS2 could move slightly out of the category of 'old and grungy looking' and into the category of 'polished and interesting', in terms of programming languages.

C#, Lua, Perl, PHP, Python, Java, JavaScript, and more, all have closures.

Tricxta 06-09-2011 03:39 AM

:0 this sounds like an awesome idea, could we have the ability to call functions from other classes as well?
example:system.map(bla);

WhiteDragon 06-09-2011 03:47 AM

Quote:

Originally Posted by Tricxta (Post 1653991)
:0 this sounds like an awesome idea, could we have the ability to call functions from other classes as well?
example:system.map(bla);

I'm not entirely sure what you're asking here, but if you're asking if the map function I wrote could be in another weapon/npc, then yes, that should be possible.

There are many interesting 'general purpose' functions like map which can be stored in a separate weapon to make your life easier. (In fact, I've already implemented a bunch of them, they just aren't as useful as they should be since we don't have closures).


All times are GMT +2. The time now is 01:59 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.