SAM identity or at least equals/hashCode

Rémi Forax forax at univ-mlv.fr
Tue Jun 21 07:17:36 PDT 2011


On 06/21/2011 04:06 PM, Peter Levart wrote:
> On 06/21/11, Brian Goetz wrote:
>>>> If you want to refer to the object instance that is the result of
>>>> SAM conversion, call that the SAM instance or the SAM object --
>>>> which is an object.  (You can't assume much about its identity --
>>>> the compiler will likely make sure that all instances of
>>>> "stateless" lambdas are really the same object, a la the
>>>> "Flyweight" pattern.)
>>> Only stateless? Why?
>> 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



More information about the lambda-dev mailing list