Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

What purpose is served by the `SerializableFunction` interface defined in Vaadin 8 if we can simply pass a regular Lambda expression in its place?

Ask Question

Vaadin 8 defines a functional interface , SerializableFunction .

This interface appears in various places. For example, when defining a Converter for displaying non-textual types in a TextField such as a UUID .

Binder.BindingBuilder<BEAN,TARGET>::withConverter(SerializableFunction<TARGET,NEWTARGET> toModel, SerializableFunction<NEWTARGET,TARGET> toPresentation)

See class documentation.

Example usage:

binder
.forField( this.idField )
.withConverter( text -> UUID.fromString( text ) , uuid -> uuid.toString() )
.bind(Panel :: getId , null );

Notice in that example that we simply pass a pair of simple Lambda expressions:

  • text -> UUID.fromString( text )
  • uuid -> uuid.toString()
  • My two questions:

  • How is it that a regular Lambda can be slipped into a place where that specific type SerializableFunction is required? How did my Lambda expression qualify?
  • Why did the Vaadin team bother defining the interface SerializableFunction if any plain old Lambda can fit in its place?
  • My aim here is not so much about the particulars of Vaadin (though I am curious there too) as it is about the Java type system, Lambdas, and functional programming.

    I understand why Vaadin web apps need every object to be serializable: In case at deployment the web container chooses to move a user session between app servers. My question is not about the serializing aspect. My question is about the Java language type mechanics, and what good does it do to add an extra interface that requires no extra coding.

    I’ll not even ask what it means for a Lambda expression to be serializable as it makes my head hurt.

    In general in Vaadin 8, the API was modernized to exploit Java 8 features. And Lambda's are the ones that are used the most. Binder is a good example of that, and some other places too. For example use of listeners becomes more fluent with Lambdas. By tradition there has been requirement for Vaadin to be possible to have sessions fully serializable, since in some special cases it is needed. Although we mostly prefer to use sticky sessions in clustered deployments, it is also possible to design Vaadin applications for high availability. In order to keep that possibility everything we use needs to be serializable. Hence there is e.g. SerializableFunction, SerializablePredicate, SerializableBiFunction, SerializableBiPredicate, SerializableConsumer, SerializableComparator, ...

    These are nothing more than "proxies" for standard Java 8 equivalents. Like for example SerializableFunction extends standard Java 8 Function and implements Serializable. Hence the it uses Lambda expression in exactly the same way.

    http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html

    I should have said in my Question that I understand the need to make everything in a Vaadin app serializable. My question is about the language type mechanics, how does my passing a regular lambda expression implement your SerializableFunction interface? – Basil Bourque Jul 15, 2018 at 6:57 Writing the lambda syntax is just a java shorthand to create a class for the developer implementing a single method interface. The compiler infers the type. This no special interfaction with Vaadin here. – cfrick Jul 16, 2018 at 5:58

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.