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

时光总嘲笑我的痴心妄想 提交于 2019-12-13 03:47:56

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/51344045/what-purpose-is-served-by-the-serializablefunction-interface-defined-in-vaadin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!