Java 8和广义目标类型推断 [英] Java 8 and Generalized Target-Type Inference

查看:91
本文介绍了Java 8和广义目标类型推断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了最新的JDK 8 ea b114来测试新的语言功能。
然而,链式调用中的推理似乎还没有奏效。

I have installed the last JDK 8 ea b114 to test the new language features. However the inference in chained calls seems not to work yet.

如果我写:

If I write:

Iterator<String> it = new ArrayList<>().iterator();

编译器给我一个错误信息。

the compiler give me an error.

然而,参数位置的推论很好用。

However inference in argument position works well.

也许不会插入链式调用中的推理?

Maybe inference in chained calls will not be inserted?

推荐答案

最新规范(公共评论)可在 jcp上找到。组织。在D部分有一段讨论了这一点。

The latest specification (Public Review) is available at jcp.org. There is a paragraph in part D which discusses this point.


接收方在方法调用,字段访问等方面(exp.foo ())不是一个聚表达式,因为目标类型是未知的 - 不可能枚举每个具有特定成员的类型(在本例中为foo)。有一些兴趣允许推断链:在()。b()中,从调用b传递类型信息到调用a。这给推理算法的复杂性增加了另一个维度,因为部分信息必须在两个方向上传递;它只适用于删除a()的返回类型对所有实例(例如List)都是固定的。这个特性不适合多表达式模型,因为目标类型不容易派生。

The receiver in a method invocation, field access, etc. (exp.foo()) is not a poly expression because the target type is unknown—it would be impossible to enumerate every type that has a particular member (foo, in this case). There has been some interest in allowing inference to "chain": in a().b(), passing type information from the invocation of b to the invocation of a. This adds another dimension to the complexity of the inference algorithm, as partial information has to pass in both directions; it only works when the erasure of the return type of a() is fixed for all instantiations (e.g. List). This feature would not fit very well into the poly expression model, since the target type cannot be easily derived.

这篇关于Java 8和广义目标类型推断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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