原文地址:
Closures in Java and Scala
翻 译:
Eastsun
People argue that verbose code is easier to understand. Do you agree when reading these two examples, one method in Java, one in Scala?
人们普遍认为,详细的代码更易于理解。但如果你阅读下面两段代码--一个使用Java另一个使用Scala--你是否还这样认为呢?
public List<Item> bought(User user) {
List<Item> result = new ArrayList();
for (Item item : currentItems) {
if (user.bought(item)) {
result.add(item);
}
}
return result;
}
def bought(user: User) = items.filter(user bought _)
If you are familiar with Java, which is more likely then you being familiar with Scala, you may tend to the Java-version. Scala has a different syntax for declaring types and generics, and supports closures, including anonymous parameters (the underscore).
如果你熟悉Java,那么很可能你将会了解到
Scala。也许你会倾向于使用Java的版本。Scala具有不同的类型声明以及泛型语法,并且支持闭包,以及匿名参数(下划线)。
When Closures are introduced in one of the next Java releases, JDK 7 or 8, the example could be rewritten like this:
在Java的后续版本中引入闭包后(JDK7或JDK8),上面Java版的代码可以重写为:
public List<Item> bought(User user) {
return ListUtils.filter(Item item : items ) {
user.bought(item);
}
}
Or with
extension methods:
或者使用
方法扩展:
public List<Item> bought(User user) {
return items.filter(Item item : ) {
// <-- note the smily! thats what its all about!
user.bought(item);
}
}
The interesting differences between Java with closures and Scala is the static typing: Scala inferences that items are all of type Item, so there is no need to specify that information again.
带有闭包的Java与Scala一个让人感兴趣的区别是它们的静态类型:Scala的类型推断能力能得到其所有变量的类型,因此不需要再指明这一点。
So, while the current Java Closures specification is a great step in the right direction, lead by Neil Gafter, who is quite the right man for the job, it may not be worth to wait for it, if you have the choice. Its not even sure yet that we'll see that spec implemented in JDK7, and even that is at best one and a half year away.
因此,即便目前的
JAVA闭包规范在
Neil Gafter的领导下正朝着正确的方向做出了很大的进步;但我们也未必需要去等待它,如果我们已经有了其他选择。何况JDK7中会不会实现闭包现在还没有确定--即便是,那也至少是一年半以后的事了。
分享到:
相关推荐
22.1 在Scala代码中使用Java名称 430 22.2 Java泛型与Scala泛型 430 22.3 JavaBean的性质 432 22.4 AnyVal类型与Java原生类型 433 22.5 Java代码中的Scala名称 433 22.6 本章回顾与下一章提要 434 ...
3.8 与Java的互操作 48 练习 49 第4章 映射和元组 A1 53 4.1 构造映射 53 4.2 获取映射中的值 54 4.3 更新映射中的值 55 4.4 迭代映射 56 4.5 已排序映射 57 4.6 与Java的互操作 57 4.7 元组 58 4.8 拉链...
jdk1.8新特性,例如:Lambda表达式(也称为闭包)是整个Java 8发行版中最受期待的在Java语言层面上的改变,Lambda允许把函数作为一个方法的参数(函数作为参数传递进方法中),或者把代码看成数据:函数式程序员对这...
Spores是Scala编译器的扩展,可以在并发和分布式环境中更安全地使用闭包。 它使开发人员可以保证基于类型的功能属性,从而可以更好地控制功能环境。 Spores带有传递检查器,以确保JVM可以对捕获的类型进行序列化...
从去年到现在,从.NET转向Java开发(只是因为项目原因,绝对与平台好坏没有关系)差不多有一年的时间了。通过这一年时间也有些感触,想从几个面比较一下这两个平台。希望能做到客观公正。我原来是使用C#语言的,和...
jgoc 编译器用 Scala 编写,运行时用 Java 编写。 Drone.io 持续集成服务 吉特聊天 为什么是JVM? 选择 JVM 的动机是: 兼容性和 JVM 调试/分析 许多优秀的库都是为 JVM 编写的。 希望通过让现有 Go 用户能够使用...
丑管理源码java 嵌入式卡夫卡 一个提供内存 Kafka 实例来运行测试的库。 灵感来自 . 版本兼容性矩阵 ...实例的代码包含在withRunningKafka闭包中。 一个例子,使用 ScalaTest: class MySpec extends An
C++、Java、Python、C#、Scala 目录 1. 数学 1.1 离散数学 1.1.1 图论 1.1.1.1 图遍历(BFS、DFS) 1.1.1.2 最小生成树(Prim、Kruskal) 1.1.1.3 最短路径(Dijkstra、Floyd) 1.1.1.4 最长路径 1.1.1.5 传递闭包 ...