Skip to content

Persisting Lambdas/Functions #286

Answered by fh-ms
tschuehly asked this question in Q&A
Discussion options

You must be logged in to vote

Unfortunately, you can not.

While, in theory, Lambdas are serializable, the JVM cannot guarantee that they will be restored properly because they don't have a unique name like classes but just contiguous numbers.

For example given following code

class Foo
{
    Consumer<String> printer = System.out::println;
}

If you store an instance of Foo, then change the code to this:

class Foo
{
    void someCode()
    {
        Consumer<String> something = (str) -> someAction(str);
    }


    Consumer<String> printer = System.out:println;
}

After restoring the Foo instance, the value for printer would be the something function because it has the first position in the code now.

It doesn't work with …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tschuehly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants