Allocation of Lambdas that capture "this"
Zhong Yu
zhong.j.yu at gmail.com
Tue Oct 14 18:36:05 UTC 2014
On Mon, Oct 13, 2014 at 1:12 PM, Neal Gafter <neal at gafter.com> wrote:
> On Wed, Aug 20, 2014 at 4:58 PM, Zhong Yu <zhong.j.yu at gmail.com> wrote:
>>
>> Is there any reason not to initialize the field directly? like
>>
>> final Consumer<Msg> readHandler = (Msg obj)-> {
>> this.doSomethingWith(obj);
>> ...
>> };
>
>
> You are not allowed to access "this" in a field initializer.
Apparently it is allowed, according to javac.
What's not allowed is to reference a final field that "might not have
been initialized" when the lambda is constructed; which I think is
unnecessarily strict and inconvenient for many legitimate use cases.
final Runnable r = ()->{ this.o.toString(); }; // javac error
final Object o; // assigned in constructor
It can be worked around by obfuscating `this`
final Runnable r = ()->{ (this).o.toString(); }; // ok
Zhong Yu
bayou.io
More information about the lambda-dev
mailing list