Graal Forums  

Go Back   Graal Forums > Development Forums > Future Improvements
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-17-2011, 04:01 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
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".
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:11 AM.


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