views:

258

answers:

4

I was at a Java conference on Scala the other day and the speaker referred to 'full powered closures'. I am having a hard time nailing down a definition that makes sense to me. I have read the wiki page on closures but it really didn't answer it for me. Can someone help me with a clear cut definition? Maybe even include a simple example.

Thanks!

+2  A: 

It's possible that the speaker used 'full powered' to mean the opposite of 'closure-like', which is what many languages actually have.

Amber
+1  A: 

As far as I know, full powered closures doesn't mean anything in particular but it may mean: simplicity of syntax. There is a world of difference between newing up an anonymous inner class in Java vs. using something like (a => a < max). Perhaps the ability to form closures on all surrounding variables not just final ones.

dpp
That example is not a closure (it doesn't capture any variables from its context). However with an example that did using a lighter weight syntax, and is a closure you would have a completely correct answer (because I also suspect the speaker was talking of easier to use syntax).
Richard
@Richard: Good point -- updated
dpp
@Richard, yes he was talking about making things less verbose was well.
northpole
Although "full powered" might mean lots if things, I think it obviously implies that it is more powerful than mere anonymous functions. Since convenient syntax doesn't make closures more powerful, I don't think that's what is meant.
Kim
@Kim: I see your point to a degree but when I believe when the syntax feels more seamless and less verbose it is more powerful in many cases.
dpp
+7  A: 

In computer science, a closure is a first-class function with free variables that are bound in the lexical environment.

You don't need more than this sentence from the wikipedia article. What Java lacks (in addition to the ugly syntax) is the feature of binding free variables from outside the closure. An example:

int x = 0;
new Runnable() {
    public void run() {
        x = 1;
    }
}.run();

Here the Java compiler will complain about the variable x.

The same thing in scala works just fine and is a lot less verbose:

var x = 0
val fun = ()=>{x=1}
fun
Kim
The Java example would only work if you'd make `x` `final` - I guess that's an example of why in Java it's not "full-powered".
Jesper
This is exactly what I was looking for. Thanks a bunch!
northpole
@Jesper: No, it wouldn't work if I made x final. I'd just get a different error message. ;) I could make x a final int array with just one element...
Kim
If you'd make `x` `final`, you can access it inside the `run()` method, but you can ofcourse not modify it, because it's `final`... Making it a one-element array will give you a surprise: in the enclosing scope, it will not be modified if you modify it inside `run()`. That's because the closure in Java is not a real closure.
Jesper
@Jesper, the modifications made to contents of the `final` array from within the `Runnable` will be visible at every scope where the array is visible.
binil
+4  A: 

Since I feel like a beginner (compared to DPP and Amber) I might explain it to a beginner in a beginners language:

First, anonymous function (or code block/lambda expression) is simply a function that does not have a name. It could be tied to a variable like this.

scala> val foo = (x: Int) => 2*x 
foo: (Int) => Int = <function1>

scala> val bar = foo
bar: (Int) => Int = <function1>

scala> bar(5)
res2: Int = 10

You see, the function doesn't have the name foo, it could be called from bar instead.

Second, a closure is an anonymous function that has a variable that is not defined inside the function (the variable/value must have been declared before the function is defined). The term "full powered closure" might refer to this functionality.

scala> var constant = 7
constant: Int = 7

scala> val foo = (x: Int) => 2*x + constant
foo: (Int) => Int = <function1>

scala> foo(5)
res3: Int = 17

scala> constant = 6
constant: Int = 6

scala> foo(5)
res4: Int = 16

First time you see this, you might wonder what it is good for. In short, it has many sectors of application :-)

olle kullberg
excellent explanation, thanks!
northpole