Java与Scala中的闭包

  [color=red]原文地址[/color]:[url=http://java.dzone.com/news/closures-java-and-scala]Closures in Java and Scala[/url]
  [color=indigo]翻  译[/color]:[url=http://eastsun.iteye.com]Eastsun[/url]


  [color=cyan]People argue that verbose code is easier to understand. Do you agree when reading these two examples, one method in Java, one in Scala?[/color]
  人们普遍认为,详细的代码更易于理解。但如果你阅读下面两段代码--一个使用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;
}

[code]def bought(user: User) = items.filter(user bought _)[/code]

  [color=cyan]If you are familiar with Java, which is more likely then you being familiar with [url=http://www.scala-lang.org]Scala[/url], 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).[/color]
  如果你熟悉Java,那么很可能你将会了解到[url=http://www.scala-lang.org]Scala[/url]。也许你会倾向于使用Java的版本。Scala具有不同的类型声明以及泛型语法,并且支持闭包,以及匿名参数(下划线)。

  [color=cyan]When Closures are introduced in one of the next Java releases, JDK 7 or 8, the example could be rewritten like this:[/color]
  在Java的后续版本中引入闭包后(JDK7或JDK8),上面Java版的代码可以重写为:
public List<Item> bought(User user) {   
return ListUtils.filter(Item item : items ) {
user.bought(item);
}
}

  Or with [url=http://www.jroller.com/scolebourne/entry/java_7_extension_methods]extension methods[/url]:
  或者使用[url=http://www.jroller.com/scolebourne/entry/java_7_extension_methods]方法扩展[/url]:
public List<Item> bought(User user) { 
return items.filter(Item item : ) {
// <-- note the smily! thats what its all about!
user.bought(item);
}
}

  [color=cyan]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.
[/color]
  带有闭包的Java与Scala一个让人感兴趣的区别是它们的静态类型:Scala的类型推断能力能得到其所有变量的类型,因此不需要再指明这一点。

  [color=cyan]So, while the current [url=http://javac.info]Java Closures specification [/url]is a great step in the right direction, [url=http://michaelhuettermann.blogspot.com/2008/03/dr-neil-gafter-jugc-and-vip.html]lead by Neil Gafter[/url], 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.[/color]
  因此,即便目前的[url=http://javac.info]JAVA闭包规范[/url]在[url=http://michaelhuettermann.blogspot.com/2008/03/dr-neil-gafter-jugc-and-vip.html]Neil Gafter的领导下[/url]正朝着正确的方向做出了很大的进步;但我们也未必需要去等待它,如果我们已经有了其他选择。何况JDK7中会不会实现闭包现在还没有确定--即便是,那也至少是一年半以后的事了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值