Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7985

Wrong "incompatible generic type" error

    XMLWordPrintableJSON

Details

    Description

      Consider the following:

      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;
      	}
      }
      

      And the following Groovy class:

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

      Compilation fails with the following error message:

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

      Attachments

        Activity

          People

            daniel_sun Daniel Sun
            mauromol Mauro Molinari
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: