Kub*_*uba 9 java lambda closures java-8
With the new java lambdas and the concept of functional interfaces, will it be possible to treat those functional interfaces as methods?
interface Func { void execute(int i); }
void call(Func f)
{
f(1); //instead of f.execute(1);
}
Run Code Online (Sandbox Code Playgroud)
I found a lot of information about the syntax of actual lambda expressions, but nothing about this.
Luk*_*der 15
你提出的建议已经在lambda-dev邮件列表中讨论过:
http://mail.openjdk.java.net/pipermail/lambda-dev/2012-February/004518.html
由于与解析器/编译器产生的歧义相关的各种问题,它主要被拒绝.或者在Brian Goetz的条款中:
我认为,最终,这只是对功能界面而不是功能类型的承诺感到不适,并试图向一个多余的中间地带进行倒退.我不认为这提供了令人信服的好处.
如果您觉得这样的"多愁善感"的功能仍然会增加Java语言的价值,那么您可以在lambda邮件列表中再次尝试运气,也许有令人信服的论据:-)
它有可能,但可能不像JavaScript中的示例语法那样优雅.根据Brian Goetz最新的lambda状态,将有章节中提到的方法参考
8方法参考
[...]
9种方法参考
[...]实际上有三种不同的方法引用,每种方法引用略有不同:
- 静态方法
- 特定对象的实例方法
- 特定类型的任意对象的实例方法
所以事实上,你的例子必须按照以下方式重新说明:
interface Func { void execute(int i); }
void call(Block<Integer> block)
{
block.run(1); //instead of f.execute(1);
}
Run Code Online (Sandbox Code Playgroud)
您现在可以传递对该execute方法的引用:
// Disclaimer: I didn't check this against a JDK8 compiler...
Func f = (i) -> { ; }; // Empty sample implementation
call(f::execute)
Run Code Online (Sandbox Code Playgroud)
换句话说,"功能"样式将在您的调用方法的声明站点实现,而不是在使用站点实现.但与JavaScript一样,use-site不必知道具体方法的名称Func.它可以只接受Blockfor返回的方法void,或者接受Callable返回值的方法.
请注意,在JDK的Mercurial存储库中,事情已经发生了变化.你将找不到run()更多,如lambda的状态所述.其他有趣的类型可以在java.util.functions包中找到:
http://hg.openjdk.java.net/lambda/lambda/jdk/file/tip/src/share/classes/java/util/function
| 归档时间: |
|
| 查看次数: |
2474 次 |
| 最近记录: |