如果我们可以简单地在其位置传递一个常规的 Lambda 表达式,那么 Vaadin 8 中定义的 `SerializableFunction` 接口有什么目的? [英] What purpose is served by the `SerializableFunction` interface defined in Vaadin 8 if we can simply pass a regular Lambda expression in its place?

查看:30
本文介绍了如果我们可以简单地在其位置传递一个常规的 Lambda 表达式,那么 Vaadin 8 中定义的 `SerializableFunction` 接口有什么目的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Vaadin 8 定义了一个 功能接口SerializableFunction.

Vaadin 8 defines a functional interface, SerializableFunction.

这个界面出现在各个地方.例如,当定义一个 Converter 用于在 TextField 例如 UUID.

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)

类文档.

示例用法:

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

请注意,在该示例中,我们只是传递了一对简单的 Lambda表达式:

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

  • text ->UUID.fromString( text )
  • uuid ->uuid.toString()

我的两个问题:

  • 如何将常规 Lambda 插入需要特定类型 SerializableFunction 的地方?我的 Lambda 表达式如何符合条件?
  • 如果任何普通的老式 Lambda 可以代替它,为什么 Vaadin 团队还要费心定义接口 SerializableFunction?
  • 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?

我在这里的目标不是关于 Vaadin 的细节(尽管我也很好奇),而是关于 Java 类型系统、Lambda 和函数式编程.

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.

我理解为什么 Vaadin 网络应用程序需要每个对象都可序列化:以防在部署时网络容器 选择在应用服务器之间移动用户会话.我的问题不是关于序列化方面.我的问题是关于 Java 语言类型机制,添加额外的 接口 不需要额外的编码.

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.

我什至不会问 Lambda 表达式可序列化意味着什么,因为它让我头疼.

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

推荐答案

总的来说,在 Vaadin 8 中,API 已经过现代化,以利用 Java 8 的特性.Lambda 是最常用的.Binder 就是一个很好的例子,其他一些地方也是.例如,使用 Lambda 可以更流畅地使用侦听器.按照传统,Vaadin 要求会话可以完全序列化,因为在某些特殊情况下需要它.尽管我们最喜欢在集群部署中使用粘性会话,但也可以设计 Vaadin 应用程序以实现高可用性.为了保持这种可能性,我们使用的所有东西都需要可序列化.因此有例如SerializableFunction, SerializablePredicate, SerializableBiFunction, SerializableBiPredicate, SerializableConsumer, SerializableComparator, ...

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, ...

这些只不过是标准 Java 8 等效项的代理".例如 SerializableFunction 扩展标准 Java 8 功能 并实现可序列化.因此它以完全相同的方式使用 Lambda 表达式.

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

这篇关于如果我们可以简单地在其位置传递一个常规的 Lambda 表达式,那么 Vaadin 8 中定义的 `SerializableFunction` 接口有什么目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆