简体   繁体   English

Java 8:泛型类型推断在方法引用上失败?

[英]Java 8: generic type inference fails on method reference?

Can anyone tell me why the following code fails to compile but the lambda version does: 任何人都可以告诉我为什么下面的代码无法编译,但lambda版本:

Are there rules about the generic type inference? 是否有关于泛型类型推断的规则? Anything I should avoid? 我应该避免什么?

Failed: 失败:

EntityLayerManager.refreshLayerRenderables(
        wwd, this.networkNodeShapeLayer, nodeMap.values(),
        MissionDetailUIConst::createNetworkNodeRenderable,
        MissionDetailUIConst::updateNetworkNodeRenderable) // <<== FAILED 

Success (with normal lambda): 成功(正常lambda):

EntityLayerManager.refreshLayerRenderables(
        wwd, this.networkNodeShapeLayer, nodeMap.values(),
        MissionDetailUIConst::createNetworkNodeRenderable,
        (e, coll) -> MissionDetailUIConst.updateNetworkNodeRenderable(e, coll));

Success (with generic parameters specified): 成功(指定了通用参数):

EntityLayerManager.<EwmsVwNetworkNodeEntity, KolladaRoot>refreshLayerRenderables(
        wwd, this.networkNodeShapeLayer, nodeMap.values(),
        MissionDetailUIConst::createNetworkNodeRenderable,
        MissionDetailUIConst::updateNetworkNodeRenderable)

Referenced Methods: 参考方法:

public static <E, R extends Renderable> int refreshLayerRenderables(WorldWindow wwd,
        RenderableLayer renderableLayer, Collection<E> entityList,
        Function<E, ? extends Collection<? extends R>> createRenderables,
        BiPredicate<E, Collection<R>> updateRenderables);

public static Collection<KolladaRoot> createNetworkNodeRenderable(EwmsVwNetworkNodeEntity networkNode);

public static boolean updateNetworkNodeRenderable(EwmsVwNetworkNodeEntity networkNode, Collection<KolladaRoot> colladaRootCollection);

This is an Eclipse bug. 这是一个Eclipse bug。 I narrowed it down to the use of a nested generic parameter ( Collection<R> ) as the type of an argument (in BiPredicate ) to the referenced method (specified as Collection<KolladaRoot> ). 将其缩小为使用嵌套泛型参数( Collection<R> )作为参数的类型(在BiPredicate )到引用的方法(指定为Collection<KolladaRoot> )。 It should compile fine in javac.exe. 它应该在javac.exe中编译正常。

I'd say stick with explicit type specification until it's fixed in 4.5 M2 , it should have less effect (if any) than switching to lambdas. 我会说坚持使用显式类型规范,直到它固定在4.5 M2中 ,它应该比切换到lambdas的效果更小(如果有的话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM