Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #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
  #2  
Old 06-09-2011, 03:19 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
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.
Reply With Quote
  #3  
Old 06-09-2011, 03:39 AM
Tricxta Tricxta is offline
The Muffin Man
Tricxta's Avatar
Join Date: Oct 2010
Location: Australia
Posts: 563
Tricxta is just really niceTricxta is just really nice
:0 this sounds like an awesome idea, could we have the ability to call functions from other classes as well?
example:system.map(bla);
__________________
Quote:
Originally Posted by Crono View Post
No look at it, Stefan is totally trolling Thor. Calling Classic a "playerworld" (something it's not supposed to be) is the ultimate subtle insult to a true fan.

It's genius.
Reply With Quote
  #4  
Old 06-09-2011, 03:47 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
Quote:
Originally Posted by Tricxta View Post
: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).
Reply With Quote
Reply


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 11:11 PM.


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