Skip to content

Instantly share code, notes, and snippets.

@danieldietrich
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldietrich/a4977c525734269348b0 to your computer and use it in GitHub Desktop.
Save danieldietrich/a4977c525734269348b0 to your computer and use it in GitHub Desktop.
What the Serializable!? #WTS

As stated by Oracle,

You can serialize a lambda expression if its target type and its captured arguments are serializable. However, like inner classes, the serialization of lambda expressions is strongly discouraged.

Interesting that the following works:

import java.util.Optional;

public class Test {

    public static void main(String[] args) {

        Optional<Object> opt = Optional.of(new Object());

        Function0<Optional<Object>> f = Function0.lift(() -> opt);

        // = "java.util.Optional"
        System.out.println(f.getType().returnType().getName());

        // = "(Ljava/util/Optional;)Ljava/util/Optional;"
        System.out.println(λ.getSerializedLambda(f).getImplMethodSignature());

    }
}

We see that Optional is a captured lambda argument and we know that it is not Serializable.

Is this good or bad?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment