You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Michael Wilkowski (JIRA)" <ji...@apache.org> on 2017/02/23 11:55:44 UTC
[jira] [Commented] (GROOVY-7985) Wrong "incompatible generic type"
error
[ https://issues.apache.org/jira/browse/GROOVY-7985?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15880309#comment-15880309 ]
Michael Wilkowski commented on GROOVY-7985:
-------------------------------------------
Another example:
{code:title=GroovyFunctionTypeInferenceTest|borderStyle=solid}
import groovy.transform.CompileStatic
import org.apache.commons.lang3.tuple.Pair
import org.junit.Test
import java.util.function.Function
@CompileStatic
class GroovyFunctionTypeInferenceTest {
@Test
void functionWithoutPairTest() {
// some function that operates on list and returns an element of this list
Function<List<String>, String> function =
{ List<Pair<String, Integer>> it -> it[0] } as Function
def list = new ArrayList<String>()
function.apply(list)
}
@Test
void functionWithPair1Test() {
// some function that operates on list and returns an element of this list
Function<List<Pair>, Pair> function =
{ List<Pair<String, Integer>> it -> it[0] } as Function
def list = new ArrayList<Pair>()
function.apply(list)
}
@Test
void functionWithPair2Test() {
// some function that operates on list and returns an element of this list
Function<List<Pair<String, Integer>>, Pair<String, Integer>> function =
{ List<Pair<String, Integer>> it -> it[0] } as Function
def list = new ArrayList<Pair<String,Integer>>()
function.apply(list)
}
}
{code}
functionWithoutPairTest() and functionWithPair1Test() compile, functionWithPair2Test() fails with the following error:
{code}
Error:(36, -1) Groovy-Eclipse: Groovy:[Static type checking] - Incompatible generic argument types. Cannot assign java.util.function.Function <java.util.List, org.apache.commons.lang3.tuple.Pair> to: java.util.function.Function <List, Pair>
Error:(38, -1) Groovy-Eclipse: Groovy:[Static type checking] - Cannot call java.util.function.Function <java.util.List, org.apache.commons.lang3.tuple.Pair>#apply(java.util.List <Pair>) with arguments [java.util.ArrayList <Pair>]
{code}
> Wrong "incompatible generic type" error
> ---------------------------------------
>
> Key: GROOVY-7985
> URL: https://issues.apache.org/jira/browse/GROOVY-7985
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Affects Versions: 2.4.7
> Reporter: Mauro Molinari
>
> Consider the following:
> {code}
> package test;
> import java.io.Serializable;
> public final class Pair<L, R> implements Serializable {
> public static <L, R> Pair<L, R> of(final L left, final R right) {
> return new Pair<>(left, right);
> }
> public final L left;
> public final R right;
> private Pair(final L left, final R right) {
> this.left = left;
> this.right = right;
> }
> }
> {code}
> And the following Groovy class:
> {code}
> package test2
> import test.Pair
> import java.util.Date;
> import groovy.transform.CompileStatic
> @CompileStatic
> class Test {
> Pair<Pair<Integer, Pair<String, Date>>, Pair<Integer, Pair<String, Date>>> doSmething() {
> def left = (Pair<Integer, Pair<String, Date>>) null
> def right = (Pair<Integer, Pair<String, Date>>) null
> return Pair.of(left, right)
> }
> }
> {code}
> Compilation fails with the following error message:
> {noformat}
> [Static type checking] - Incompatible generic argument types. Cannot assign test.Pair <test.Pair, test.Pair> to: test.Pair <Pair, Pair>
> @ line 15, column 10.
> return Pair.of(left, right)
> {noformat}
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)