Lambdas and serialization
alessiostalla at gmail.com
Wed Oct 27 04:57:41 PDT 2010
On Wed, Oct 27, 2010 at 12:48 PM, Llewellyn Falco <isidore at setgame.com> wrote:
> Everybody seems to be thinking of lambdas from the "I want to create a
> lambda" side of the fence.
> allow me to comment from the "I want to consume a lambda" side of the fence.
> As I understand it the method
> <T> T max(List<T> from, Comparable<T> c)
> should be invokable via a lambda?
> if this is correct, then by the time the lambda reaches this consuming
> method, the fact it was created by lambda should *no longer be relevant*.
> By this logic, if the rules for a consuming method signature would allow for
> the parameter to be serialized, then the lambda should conform to those
In general I agree, but Serializable is a special case. It is a marker
interface and, strictly speaking, it does not specify an interface
(i.e. an API); the implementing object might be or might be not
serializable. An example: collections generally implement
Serializable, however, if you try to serialize a collection containing
non-serializable items, it'll fail.
SAM-conversion only ensures that lambdas respect the target type's
API; it doesn't limit the runtime behavior in any way, so it's allowed
for a lambda to not be serializable even if the target is. Of course,
it would be better if lambdas were always serializable, but that's a
tradeoff between functionality and implementers' freedom to optimize.
More information about the lambda-dev