Roberto Attias
Roberto Attias

Reputation: 1903

Generics with capture, can't convert

Consider the following data type:

import java.util.ArrayList;
import java.util.List;

public class MyList<T extends Comparable<T>> {
  protected List<T> data = new ArrayList<>();

  public void add(T v) {
    data.add(v);
  }
}

And let's define a generic interval data type:

public interface IInterval<T extends Comparable<T>> extends Comparable<IInterval<T>> {

  T getBegin();

  T getEnd();

  @Override
  default public int compareTo(IInterval<T> other) {
    int c1 = getEnd().compareTo(other.getEnd());
    if (c1 == 0) {
      return getBegin().compareTo(other.getBegin());
    } else
      return c1;
  }
}

Suppose we want to extend MyList to hold intervals, and have a method that returns interval intersecting a certain range:

import java.util.ArrayList;
import java.util.List;

public class MyIntervalList<T extends Comparable<T>> extends MyList<IInterval<T>> {

  public List<? extends IInterval<T>> getIntersect(T begin, T end) {
    List<IInterval<T>> res = new ArrayList<>();
    for (IInterval v : data) {
      if (v.getEnd().compareTo(begin) < 0 ||
          v.getBegin().compareTo(end) > 0) {
        res.add(v);
      }
    }
    return res;
  }
}

Let's then have an example of use:

import java.util.List

class Interval implements IInterval<Integer> {
  private int begin, end;

  public Interval(int begin, int end) {
    this.begin = begin;
    this.end = end;
  }

  public Integer getBegin() {
    return begin;
  }

  public Integer getEnd() {
    return end;
  }
}

public class Test {
  public static void main(String[] args) {
    MyIntervalList<Integer> t = new MyIntervalList<>();
    t.add(new Interval(1, 10));

    List<Interval> l = t.getIntersect(1, 4);
  }
}

When I compile this I get the error:

Test.java:8: error: incompatible types: List<CAP#1> cannot be converted to List<Interval>
    List<Interval> l = t.getIntersect(1, 4);
                                     ^
  where CAP#1 is a fresh type-variable:
    CAP#1 extends IInterval<Integer> from capture of ? extends IInterval<Integer>
1 error

As Interval implements IInterval<Integer> and getIntersect is declared as returning ? extends IInterval<T>, where Integer has been passed as T, I thought this was the correct way to do this, but apparently I'm wrong.

Can anybody explain the correct way?

Upvotes: 1

Views: 248

Answers (1)

teppic
teppic

Reputation: 7286

First tweak MyList to allow it to accept anything (it doesn't need to restrict types to Comparable):

class MyList<T> { 
    protected List<T> data = new ArrayList<>();

    public void add(T v) {
        data.add(v);
    }
}

Then change the signature of MyIntervalList to specify the type of interval it contains:

class MyIntervalList<T extends Comparable<T>, I extends IInterval<T>> extends MyList<I> {
    public List<I> getIntersect(T begin, T end) {
        List<I> res = new ArrayList<>();
        for (I v : data) {
            if (v.getEnd().compareTo(begin) < 0 ||
                v.getBegin().compareTo(end) > 0) {
                res.add(v);
            }
        }
        return res;
    }
}

Now the compiler can tell what type of interval it's dealing with, and will be happy:

public static void main(String[] args) {
    MyIntervalList<Integer, Interval> t = new MyIntervalList<>();
    t.add(new Interval(1, 10));
    List<Interval> intersect = t.getIntersect(1, 4);
}

Upvotes: 1

Related Questions