How can I elegantly serialize a lambda?
For example, the code below throws a NotSerializableException
. How can I fix it without creating a Seriali
Very ugly cast. I prefer to define a Serializable extension to the functional interface I'm using
For example:
interface SerializableFunction extends Function, Serializable {}
interface SerializableConsumer extends Consumer, Serializable {}
then the method accepting the lambda can be defined as such :
private void someFunction(SerializableFunction function) {
...
}
and calling the function you can pass your lambda without any ugly cast:
someFunction(arg -> doXYZ(arg));