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?