transparent lambda

Peter Levart peter.levart at gmail.com
Wed Dec 30 13:45:34 PST 2009


Isn't it true that method 'four' only calls lambda 'two' once? Lambda
'two' returns from method 'four' (since lambda is transparent) before
the second call.
My question was about how can a program differentiate multiple
invocations of a method from the same call site? Does it maintain a
counter?

Regards, peter


2009/12/30, Neal Gafter <neal at gafter.com>:
> 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
>
> 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.
>
> Cheers,
> Neal
>

-- 
Poslano z moje mobilne naprave


More information about the closures-dev mailing list