質問

I have the following piece of code:

public interface Segment<T> extends Period { ... };

public class SegmentImpl_v1<T> implements Segment<T> { ... };


public interface TimeLine<T, S extends Segment<T>> { ... };

public class TimeLineImpl<T, S extends Segment<T>>
        implements TimeLine<T, S> {

    private SortedSet<S> segments = new TreeSet<S>();

    public void someFunction() {

        // no suitable method for...
        segments.add(new SegmentImpl_v1<T>(...)); 

    }

}

and I get a no suitable method for... when adding a segment instance. It seems like Java's treeset does not apply the PECS principle. Is there a solution to this issue?

SOLUTION

I implemented:

public static <T> Segment<T> newItem(Period p, T itemValue) {
    return new SegmentImpl_v1(p, itemValue);
}

in SegmentImpl_v1 and call it in someFunction().

役に立ちましたか?

解決

PECS has nothing to do with this. (You don't have any bounded wildcards.)

The problem is SegmentImpl_v1<T> is not a subtype of S. When you pass an argument to a method it must be a subtype of the declared type of the parameter.

他のヒント

Not seeing the relationship to PECS. Your troublesome code is not producing or consuming the generic collection. You're just working with it. The collection is type S. Of course you can't put a SegmentImpl_v1 into it. What if someone did this:

new TimelineImpl<Foo, SegmentImpl_v2<Foo>>  

Allowing a SegmentImpl_v1 in a SortedSet there is clearly incorrect.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top