What is the meaning of this?
Mark Mahieu
markmahieu at googlemail.com
Thu Jan 28 14:07:38 PST 2010
On 28 Jan 2010, at 21:46, Peter Levart wrote:
> On Thursday 28 January 2010 20:19:05 Joshua Bloch wrote:
>> Entirely banning the use of this in lambada expressions is a very bad idea.
>> It amounts to throwing out the baby with the bath water. If you need to get
>> your hands on the function object created by a lambda expression, but
>> this doesn't
>> provide it, you're out of luck.There is no general-purpose workaround. And
>> people will be tempted to use workarounds that aren't thread-safe, such as
>> stashing the lambda in a field.
>>
>
> Local variables are perfectly thread-safe:
>
> void method()
> {
> final #int(int) factorial = #(int i)(i == 0 ? 1 : i * factorial.(i - 1));
> }
>
> This can be made allowed, since evaluation of lambda expression is not using the value of final
> variable being defined, it's just capturing it and there are no recursive type resolving problems
> here.
>
> Peter
>
Yep, that's how I used to do it with BGGA for example. It seemed obvious enough at the time that I never even considered 'stashing it in a field'.
I guess that wouldn't work when using a self-referencing lambda as an argument to a super-constructor call. Still, that's not a problem specific to lambdas, and the possible 'solutions' (which might include let-expressions) probably have wider applicability too.
Mark
BTW, I had a very bizarre mental image when I first scanned over Josh's email and saw the phrase "stashing a lambda in a field". It involved a shovel.
More information about the lambda-dev
mailing list