Java 8: generic type inference fails on method reference?

913 views Asked by At

Can anyone tell me why the following code fails to compile but the lambda version does:

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):

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);
1

There are 1 answers

0
Sean Van Gorder On BEST ANSWER

This is an 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>). It should compile fine in 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.