Effectively final
Brian Goetz
brian.goetz at oracle.com
Sat Jul 30 09:16:25 PDT 2011
>> void foo(AsyncSocket socket) {
>> int[] timeout = {false };
>> AsyncSystem.setTimeout(100, #{ timeout[0] = true; });
>> socket.onData(#{ socket -> if (!timeout[0]) socket.write("Got data");
>> });
>> }
> I would consider the use of an array like that as ugly cruft.
Yes, Remi should have known better :)
> And even
> worse, if this was multi-threaded (which thankfully it isn't) it would
> still be subject to race conditions, which goes to show that requiring
> final locals in closures doesn't actually protect you from races since
> you can do ugly workarounds like this (or just use some final ref to an
> arbitrary object) ;)
This argument has been extensively raised, rejected, reraised, and
re-rejected: just because any safety mechanism can be subverted does not
mean that such safety mechanisms are silly.
More information about the lambda-dev
mailing list