Regarding closures
After reading/watching Stuart's nice slides on Closures in the context of JavaScript, I have started to like JavaScript. Personally, I don't accept any language as a high-level scripting language if it doesn't support closures. Python is therefore straight out of my window. Although Vala isn't a scripting language, it would be nice to have such support in there as well. It already supports lambda functions with no restrictions and Jürg has concrete plans to support closures, it's more a matter of when rather than why or how. When that support is there, just try and stop me from loving Vala. :)
UPDATE: Thanks to Anonymous, I now stand corrected that Python does fully support closures. Although I still don't like the fact that it restricts lambda functions to be one-liner but at least it's not straight out of the window anymore. :)
UPDATE#2: Andy Wingo informs me that python doesn't really fully support closures. He even put up a small code fragment to make his point. So I hereby throw python out the window, again. :)
UPDATE: Thanks to Anonymous, I now stand corrected that Python does fully support closures. Although I still don't like the fact that it restricts lambda functions to be one-liner but at least it's not straight out of the window anymore. :)
UPDATE#2: Andy Wingo informs me that python doesn't really fully support closures. He even put up a small code fragment to make his point. So I hereby throw python out the window, again. :)
Comments
The wikipedia article has an example:
http://en.wikipedia.org/wiki/Closure_(computer_science)
(PS. one existing compiled language that supports anonymous functions and full closures is D 2.0. D 1.0 only supports "partial" closures, it will fail if one tries to refer to stack-allocated data in closures.)
Yeah, thanks for the info. I just updated my blog entry. :)
one existing compiled language that supports anonymous functions and full closures is D 2.0.
Hmm.. The thing is that I already love Vala for various other reasons so support for full closures will only increase the amount of love and respect for Vala. :)
(function(really_local_i) { ... })(i);
Andy
So you love Vala and js, great speak about it, no problem. So be positive about them. Why are you comparing everything to python if you just hate it so much ? Forget it altogether, get over it.
Example:
>>>def foo(x):
... def bar(x,y):
... return x*y
... return lambda y: bar(x,y)
This works perfectly and is not to quirky:)
You can duplicate this behavior without any closures, using global variables:
x = 0
def f(y):
if y: return x
x = y
return x
------
In addition, anonymous functions (AKA lambdas) are not as crippled as you might think. They may only include one expression, but that's true of many other lambda-using languages as well (lisp, scheme, etc).
Could you please stop reading my blog if you get so upset about me bashing python. I do that to make a big bunch of GNOME developers realize that contrary to their beliefs python is far from being a perfect language.
It has everything to do with closures, because x should be the variable closed over. Python borks and assumes it's a local variable when you try to assing something to it, because the lexical bindings in a closure are read-only.
In addition, anonymous functions (AKA lambdas) are not as crippled as you might think. They may only include one expression, but that's true of many other lambda-using languages as well (lisp, scheme, etc).
It's intellectually dishonest to pretend you know anything about lisp when you obviously have no clue about it.
You have that the wrong way round, I think. The lexical bindings in a closure are read only, because Python currently assumes that the first assignment in a function creates a new local variable.
Python 3 will include a new keyword "nonlocal", to specify that assignments to a particular variable rebind the name from the enclosing scope, rather than creating a new variable.
The lambda keyword is simply syntatic sugar for defining functions. If you want to do something the lambda syntax doesn't allow, use the standard function syntax.
As for altering variable bindings from parent scopes in closures, that is coming in Python 3.0 with the "nonlocal" keyword. Until then, you can work around this by using mutable variables such as lists (which don't require rebinding the variable name to another object).
http://www.paulgraham.com/icad.html
Closures are all about whether your language does the footwork of closing the functions for you.
Stopping halfway like Python does because of the stupid "implicit variable declaration on first assignment" feature are, in my book, signs of weakness in a language, and it irritates me.
- mvo
That's a bit misleading. Lisp/Scheme can have more than one expression in a lambda. But even if that were correct, limiting to a single expression in lisp/scheme would not be a major limitation anyway because all of the control flow statements such as loops and if-statements are actually just expressions. So you can have any amount of complexity in a single expression.
Good to know! Until then we can say that Python doesn't fully support closures. :)