Updated State of the Lambda
Peter Levart
peter.levart at marand.si
Mon Oct 18 08:04:07 PDT 2010
On 10/18/10, Rémi Forax wrote:
> > Why could the compiler not recognise the use of (lambda) this and
> > provide it as an argument as you suggest?
>
> Because it's not the same object. If you mutate a method handle, you
> create a new one
> due to its immutable nature.
>
> MethodHandle mh = #{ MethodHandle anotherMH -> mh == anotherMH };
> mh.invokeGeneric(mh) // false
>
> because, it's translated to:
>
> static boolean lambda$1(MethodHandle mh, MethodHandle anotherMH) {
> return mh == anotherMH;
> }
>
> MethodHandle mh = #lambda$1;
> mh = mh.bindTo(mh);
>
> mh.invokeGeneric(mh); // return mh == #lambda$1 => false
>
If the identity of MethodHandle matters, another trick could be:
static boolean lambda$1(MethodHandle[] mh, MethodHandle anotherMH) {
return mh[0] == anotherMH;
}
MethodHandle mh = #lambda$1;
MethodHandle[] temp$1 = new MethodHandle[1];
mh = mh.bindTo(temp$1);
temp$1[0] = mh;
mh.invokeGeneric(mh); // return mh == #lambda$1 => true
...for the price of another object on the heap.
Peter
More information about the lambda-dev
mailing list