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=134257119)

WhiteDragon 11-29-2009 09:07 PM

Function Closures
 
Inline Closures
These would be useful to make code a bit cleaner:
PHP Code:


foobar
(function (temp.atemp.b) {
  return 
temp.temp.b;
}); 


In-place Execution
This would be useful for proper scoping (since we don't have block scope):
PHP Code:


temp
.= function () {
  
temp.foo 3;
  
temp.bar 50;
  
temp.baz 4;
  return 
temp.foo temp.bar temp.baz;
}(); 


WhiteDragon 12-12-2009 12:52 AM

Another cool thing would be binding variables to the closure during creation:
PHP Code:

function foo() {
  
temp.2;
  
temp.= function (temp.xtemp.y) use (temp.a) {
    return (
temp.temp.y) * temp.a;
  };
  
this.bar(temp.f3);
}

function 
bar(temp.ctemp.d) {
  echo(
temp.c(4temp.d)); // would echo 14



Admins 12-12-2009 01:21 AM

Quote:

Originally Posted by WhiteDragon (Post 1541223)
Inline Closures
These would be useful to make code a bit cleaner:
PHP Code:


foobar
(function (temp.atemp.b) {
  return 
temp.temp.b;
}); 


You mean instead of doing this?
PHP Code:

temp.func = function (temp.atemp.b) {
  return 
temp.temp.b;
};
foobar(temp.func); 


WhiteDragon 12-12-2009 01:30 AM

Quote:

Originally Posted by Stefan (Post 1543800)
You mean instead of doing this?
PHP Code:

temp.func = function (temp.atemp.b) {
  return 
temp.temp.b;
};
foobar(temp.func); 


Yes.

In a script where I created 40 to 50 different closures it was getting irritating to create a junk variable like that.

Skyld 12-12-2009 01:51 AM

Quote:

Originally Posted by WhiteDragon (Post 1543803)
Yes.

In a script where I created 40 to 50 different closures it was getting irritating to create a junk variable like that.

It is not really a junk variable because when you define a function normally, you are still creating a variable which contains the function object without realising it. In any other case, what other language uses that kind of syntax that you suggested? It is horrendously ugly.

WhiteDragon 12-12-2009 02:31 AM

Quote:

Originally Posted by Skyld (Post 1543805)
It is not really a junk variable because when you define a function normally, you are still creating a variable which contains the function object without realising it.

It is a junk variable because I create it, and only use it one time.

Quote:

Originally Posted by Skyld (Post 1543805)
In any other case, what other language uses that kind of syntax that you suggested?

Any C-like language with support for closures/anonymous functions. Try ECMAScript (JavaScript, ActionScript, QtScript, Objective-J), or PHP.

All the functional languages such as Haskell, Erlang, LISP, and Scheme also allow defining the function inline, so the concept is not bogus either.

Loriel 12-12-2009 02:09 PM

Quote:

Originally Posted by WhiteDragon (Post 1543816)
It is a junk variable because I create it, and only use it one time.

Maybe you could reuse it for the next time you need an anonymous function!!

Quote:

Any C-like language with support for closures/anonymous functions. Try ECMAScript (JavaScript, ActionScript, QtScript, Objective-J), or PHP.
Man, I want to agree with this thread on principle, but after you calling PHP C-like, I really just want to punch you in the face with a monoid in the category of endofunctors.

Worth adding that lua has the almost the same syntax except without the curly braces because not having curly braces there is what lua does, C# has almost the same syntax except with delegate instead of closure because calling something anonymous a function scares OOP programmers, etc. Perl has the same kind of feature except without a parameter list and yet another keyword.

Quote:

All the functional languages such as Haskell, Erlang, LISP, and Scheme also allow defining the function inline, so the concept is not bogus either.
With GS2 basically looking like Javascript, that is the only comparison there is. If people feel that adding Javascript's syntax for anonymous functions is not worth it, comparing to even crazier languages is not going to help, I suspect.

WhiteDragon 01-30-2010 02:22 AM

Bump.

This could be used on the iPhone too!

12171217 01-30-2010 02:39 AM

Quote:

Originally Posted by WhiteDragon (Post 1553142)
This could be used on the iPhone too!

so?

Immolate 01-30-2010 03:03 AM

I'd like this to be added in but I can't see where average scripters would use it, so putting my self in Stefan's shoes, it'd probably never implemented or really low priority.

coreys 01-30-2010 05:38 AM

Quote:

Originally Posted by 12171217 (Post 1553143)
so?

It's the only way to get something done by Stefan these days, it seems.

LoneAngelIbesu 01-30-2010 07:52 PM

Quote:

Originally Posted by coreys (Post 1553172)
It's the only way to get something done by Stefan these days, it seems.

:rolleyes: Certainly making comments like this will help!


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

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