This has probably been asked before in some form or other but I still can't solve it so I thought I'd ask the collective wisdom here. I have an interface like so - I left in some of the comments as they may be of help.
/** Technical note: this generic binding is known as F-bound where class
* T is 'the worked-on class' by Splittable i.e. the implementing class.
*/
interface Splittable <T extends Element & Splittable<T>> {
/** Returns an object of the same class which has been split off at
* the point given.
*/
public T splitOff(double at);
/** Appends the object provided, which must be of the same class, to
* this object.
*/
public void append(T el);
}
So then I'm working on an Element which I don't know at compile time thus:
if (el instanceof Splittable) {
if (nextEl.getClass == el.getClass) {
// same class so join them
((Splittable)el).append(nextEl);
}
}
and that's where I get a compiler warning - unchecked call to append(T) as a member of the raw type Splittable.
In the Element subclass I have tried both
SubClassEl extends Element implements Splittable
and
SubClassEl extends Element implements Splittable<SubClassEl>
with no difference in warning.
Also, at the point of calling append(), I tried
((Splittable)el).splitOff(x).append(nextElement)
but the compiler doesn't recognise that splitOff should return a Splittable, only an Element.
Any ideas?