Effectively final
Serge Boulay
serge.boulay at gmail.com
Wed Sep 14 12:48:32 PDT 2011
not even through a reference such as an atomic boolean ? (yes, I know Brian
frowns on this stuff)
void foo(final Socket socket) {
AtomicBoolean timedOut = new AtomicBoolean(false);
system.setTimeout(100, ->{ timedOut.set(true); });
socket.onData(->{ if (!timedOut.get()) System.out.println("Got data");
});
}
On Wed, Sep 14, 2011 at 2:57 PM, Neal Gafter <neal at gafter.com> wrote:
> 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