SAM identity or at least equals/hashCode
Peter Levart
peter.levart at marand.si
Tue Jun 21 07:31:29 PDT 2011
On 06/21/11, Rémi Forax wrote:
> >> Lambdas can capture final local variables. Consider:
> >>
> >> public IntFactory constGenerator(int x) { return #{ -> x } };
> >>
> > This is exactly the use case I'm talking about. One would want that
> >
> > constGenerator(12).equals(constGenerator(12)) would evaluate to true and
> > constGenerator(12).equals(constGenerator(13)) would evaluate to false
> >
> > event though the instances of two SAM objects returned from two invocations of method:
> >
> > constGenerator(12)
> >
> > are not the same instance, they could be equal.
>
> and with:
>
> public IntFactory constGenerator1(int x, int y) { return #{ -> x + y } };
> public IntFactory constGenerator2(int x, int y) { return #{ -> x + y } };
> public IntFactory constGenerator3(int x, int y) { return #{ -> x - y } };
>
>
> constGenerator1(2, 3) == constGenerator2(2, 3) ??
> constGenerator1(2, 3) == constGenerator3(2, 3) ??
>
> Rémi
...by my suggestion, they would be not equal. I think that defining the identity of the code block that executes lambda body in terms of the lexical instance in source code is doable (it maps to what compiler does) and can easily be explained in the spec. Defining the equality in terms of code structure would be to much, I think.
Regards, Peter
More information about the lambda-dev
mailing list