SAM identity or at least equals/hashCode
Brian Goetz
brian.goetz at oracle.com
Tue Jun 21 06:20:19 PDT 2011
>> I think "lambda expression" is the right thing to say when you are
>> referring to the lambda itself. It is an expression, but its type
>> is contingent on its context (it is target-typed) and undergoes a
>> conversion of sorts before it becomes a SAM.
>>
>> 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 } };
Clients will call the resulting IntFactory with
foo.make()
and expect an int to come back. The SAM-converted lambda is an object,
it has to have a nilary make() method, that method has captured state x,
so where do we store the captured state?
> The question is: Will SAM objects that lambda expressions are going
> to be converted to, implement any meaningfull equals/hashCode methods
> or are those methods going to be inherited from Object (or SAM
> class).
No.
> "If a SAM type is a class type, then it must be declared abstract and
> have a no-args constructor."
Class SAMs are no longer under consideration, only interface SAMs.
More information about the lambda-dev
mailing list