Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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);
like image 946
jiping-s Avatar asked Sep 30 '22 02:09

jiping-s


1 Answers

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.

like image 54
Sean Van Gorder Avatar answered Oct 04 '22 18:10

Sean Van Gorder