Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Lexically Binding a Function (https://forums.graalonline.com/forums/showthread.php?t=134258232)

WhiteDragon 03-02-2010 02:36 AM

Lexically Binding a Function
 
1 Attachment(s)
I was playing with function closures in GS2, trying to make them do something useful, and after hacking away at the callstack for awhile, I finally managed to do this.

PHP Code:

join("utility_bind");
//#CLIENTSIDE
function foobar(temp.atemp.b) {
  
temp.5;
  
temp.baz = function (temp.x) {
    
temp.scope this.getScope();
    return 
temp.scope.temp.scope.temp.scope.temp.x;
  };
  
this.bind(temp.baz);
  return 
temp.baz;
}
function 
onCreated() {
  
temp.this.foobar(510);
  echo(
temp.f(5)); // echos 25


(Beware that copying this directly will add a space after every line, screwing up //#CLIENTSIDE)

Basically, you can bind the current state of the outer function (foobar) to any function you pass (temp.baz), and it will be immutable until the bound function executes this.getScope() and stores it.


The magic is attached.


getCallStack() only works clientside in v6, so you'll need that to see this in action.

coreys 03-02-2010 03:46 AM

Quote:

Originally Posted by WhiteDragon (Post 1560063)
For some reason getCallStack() isn't working in v5 so until that is fixed this only works in v6.

getCallStack() works fine for me in v5. :confused:

WhiteDragon 03-02-2010 03:48 AM

Quote:

Originally Posted by coreys (Post 1560082)
getCallStack() works fine for me in v5. :confused:

Discovered that it is only clientside for v6, but also exists serverside (for both versions obviously).

coreys 03-02-2010 05:15 AM

Quote:

Originally Posted by WhiteDragon (Post 1560084)
Discovered that it is only clientside for v6, but also exists serverside (for both versions obviously).

Oh, no wonder. I've never tried using it clientside.


All times are GMT +2. The time now is 12:33 PM.

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