Effectively final

Neal Gafter neal at gafter.com
Wed Sep 14 11:57:02 PDT 2011


A lambda can't modify a local variable from the enclosing scope.

On Wed, Sep 14, 2011 at 11:46 AM, blackbelt1999 <blackbelt1999 at sbcglobal.net
> wrote:

> Is there not a fundamental difference between the original code and the new
> code, namely the original code creates a new thread of execution while the
> new
> code does not? If so, can't the new code be simplified to:
>
>   void foo(final Socket socket) {
>        boolean timedOut = false;
>       system.setTimeout(100, () =>  { timedOut = true; });
>      socket.onData(() =>  { if (!timedOut) System.out.println("Got data");
> });
>   }
>
> The above version .does not go through the expense of creating a new object
> while providing the same behavior?
>
> --Alan
>
>
>
>
> ________________________________
> From: Steven Simpson <ss at comp.lancs.ac.uk>
> To: lambda-dev at openjdk.java.net
> Sent: Wed, September 14, 2011 4:10:42 AM
> Subject: Re: Effectively final
>
> Just a loose end...
>
> On 15/08/11 15:41, Tim Fox wrote:
> > On 15/08/2011 15:22, Steven Simpson wrote:
> >>       void foo(final Socket socket) {
> >>         new Runnable() {
> >>           boolean timedOut;
> >>           public void run() {
> >>             system.setTimeout(100, #{ timedOut = true; });
> >>             socket.onData(#{ if (!timedOut) System.out.println("Got
> data");
> >});
> >>           }
> >>         }.run();
> >>       }
> > Steven, firstly thanks for putting in the effort to look at this. And
> > kudos for your ingenuity :)
>
> And yet I missed this trick!:
>
>   void foo(final Socket socket) {
>     new Object() {
>       boolean timedOut;
>       void run() {
>         system.setTimeout(100, () ->  { timedOut = true; });
>         socket.onData(() ->  { if (!timedOut) System.out.println("Got
> data");
> });
>       }
>     }.run();
>   }
>
> So the object doesn't have to be Runnable, and run() doesn't have to be
> public, return void, or be called run!  That's more convenient if you
> want to return a value, rather than doing tricks with Callable:
>
>   return new Object() {
>     int run() { return 0; }
>   }.run();
>
> Cheers,
>
> Steven
>
>


More information about the lambda-dev mailing list