Lambdas and serialization

Maurizio Cimadamore maurizio.cimadamore at
Tue Oct 19 07:48:29 PDT 2010

On 19/10/10 15:13, Paul Benedict wrote:
> Does the SAM specification say anything about it must implement ALL
> the interfaces in the SAM wrapp? Or is that simply not decided on yet?
There shouldn't be differences between an instance of a SAM type created 
using 'new' and another instance obtained via lambda conversion. Which 
means, if the SAM type S is serializable, so should be the instance of S 
obtained when SAM-converting a lambda expression to the target type S.

The fact that this might be difficult to achieve should the translation 
strategy exploit method handles is something that can be regarded as an 
implementation problem, not a design one.

> On Tue, Oct 19, 2010 at 9:10 AM, Maurizio Cimadamore
> <maurizio.cimadamore at>  wrote:
>> On 19/10/10 14:41, Alessio Stalla wrote:
>>> On Tue, Oct 19, 2010 at 3:30 PM, Paul Benedict<pbenedict at>
>>>   wrote:
>>>> Will the wrapper be serializable if the SAM interface is? That's a
>>>> good question, but I imagine it has to be because it is implementing
>>>> the interface.
>>>> public interface MySamType extends Serializable {
>>>>   void doSomething(int x);
>>>> }
>>> Sorry, I was unclear with the expression "be serializable": what I
>>> really meant is not if it will merely implement,
>>> rather if it will actually be serialized by an ObjectOutputStream
>>> without any exception in all cases where a "regular" instance of the
>>> SAM would have been serialized without exceptions, and deserialized in
>>> the same conditions as well. That's imho a desirable feature to have,
>>> but it places a possibly non-trivial burden on the implementation.
>> Good point. The short answer to this question is: it depends on how lambda
>> expressions are translated by the compiler. If the compiler uses anonymous
>> inner classes to translate away lambda expression, then the answer is yes.
>> If lambda expressions are to be translated away by using method handles,
>> then I *guess* the answer is no, as it seems like MethodHandle are not
>> serializable (Remi or John might know more about this though).
>> Maurizio

More information about the lambda-dev mailing list