M
Murat Tasan
i have a generic interface defined as:
public interface DistanceFunction<S, T>
{
public <D extends Number & Comparable<D>> D computeDistance(S source, T
trget);
}
and this compiles just fine. note how i want to return a Number that is
guaranteed to be comparable to itself.
now i am trying to implement this with a simple example:
public class TestDistanceFunction implements DistanceFunction<Object,
Object> {
public Double computeDistance(Object source, Object target) {
return new Double(69.0d);
}
}
everything should be fine at this point i think. in fact it compiles just
fine. the generic type variables used in the interface (S and T) are
correct and give no problem. but when i compile with all warnings on, i
get the following warning:
sourcepath/TestDistanceFunction.java:3: warning:
computeDistance(java.lang.Object,java.lang.Object) in TestDistanceFunction
implements <D>computeDistance(S,T) in mur.math.DistanceFunction; return
type requires unchecked conversion found : java.lang.Double
required: D
public Double computeDistance(Object x, Object y)
^
1 warning
now, all i want is for any implementing class of the DistanceFunction
interface to return a Number that is comparable to itself. Double
certainly satisfies this. using D in the generic method declaration
simplifies the generic interface (i.e. leaving D out of the generic
interface type variables), but it is only used to indicate the range of
possible return values from the method.
does anyone have any elegant ideas as to how to get rid of this warning?
note that "elegant" means NOT including D as one of the generic interface
type variables, as its only purpose is a place-holder for the return type
of that single method.
thanks much for any insight,
murat
public interface DistanceFunction<S, T>
{
public <D extends Number & Comparable<D>> D computeDistance(S source, T
trget);
}
and this compiles just fine. note how i want to return a Number that is
guaranteed to be comparable to itself.
now i am trying to implement this with a simple example:
public class TestDistanceFunction implements DistanceFunction<Object,
Object> {
public Double computeDistance(Object source, Object target) {
return new Double(69.0d);
}
}
everything should be fine at this point i think. in fact it compiles just
fine. the generic type variables used in the interface (S and T) are
correct and give no problem. but when i compile with all warnings on, i
get the following warning:
sourcepath/TestDistanceFunction.java:3: warning:
computeDistance(java.lang.Object,java.lang.Object) in TestDistanceFunction
implements <D>computeDistance(S,T) in mur.math.DistanceFunction; return
type requires unchecked conversion found : java.lang.Double
required: D
public Double computeDistance(Object x, Object y)
^
1 warning
now, all i want is for any implementing class of the DistanceFunction
interface to return a Number that is comparable to itself. Double
certainly satisfies this. using D in the generic method declaration
simplifies the generic interface (i.e. leaving D out of the generic
interface type variables), but it is only used to indicate the range of
possible return values from the method.
does anyone have any elegant ideas as to how to get rid of this warning?
note that "elegant" means NOT including D as one of the generic interface
type variables, as its only purpose is a place-holder for the return type
of that single method.
thanks much for any insight,
murat