transparent lambda

Stefan Schulz schulz at the-loom.de
Wed Dec 30 10:47:26 PST 2009


On 30.12.2009 17:42, Neal Gafter wrote:
> I'm trying to avoid a "solution" that allows this puzzler to be puzzling:
>
> *#int() two = null;
> synchronized int four() {
>    if (two == null) two = #() { return 2; };
>    return two() + two();
> }
> public static void main(String[] args) {
>    System.out.println(four());
> }
> *
>
> What does this do?
>
>    1. prints "4"
>    2. throws an exception
>    3. prints "2"
>    4. Sometimes prints "2", sometimes throws an exception
>    5. does not compile

Choice 5, as four is not static ;)
With the current syntax planned for lambdas, Choice 1 would be correct 
and to me the least surprising one (no puzzler at all).

> If you make statement lambdas transparent, the answer is choice 4: the
> method prints "2" on its first execution, and throws an exception
> (unmatched transfer) thereafter.

So you'd bind the creation of the lambda to the actual first run of the 
method? Why is that? IMHO, if it were the same instance and the same 
method, it should just work fine, as long as it is invoked from within 
the method's scope.

Stefan


More information about the closures-dev mailing list