Serialization

Alex Blewitt alex.blewitt at gmail.com
Wed Dec 16 13:06:17 PST 2009


On Wed, Dec 16, 2009 at 3:46 PM, Joshua Bloch <jjb at google.com> wrote:
> Associating a class name with a closure is only half the battle.  Loosely
> speaking, it takes you from serializing an anonymous class instance to a
> named *non-static* nested class.  But even the latter is generally
> considered a bad idea, because you end up serializing the enclosing
> instance, which is rarely what you want. The serializable SAM interfaces
> that we use in MapReduce are typically nested *static *classes (hence no
> enclosing instance). I'd argue that this is a reasonable restriction for
> serializable function objects (the static part, not the nested part).

I agree that not all lambdas should be serializable. Are we talking
'static' as defined by the compiler, or by the developer in the
source? I'd argue that a good compiler should be able to promote (for
want of a better description) 'instance' closures to 'static' closures
automatically iff the appropriate conditions hold (no references to
this variables etc.). In that case, one would be able to use #int(int
a) { return a*42; } as a serializable lambda, regardless of whether it
was defined (in the source) as a static, instance, or stack-based
variable.

Alex


More information about the lambda-dev mailing list