W
William
class IfThisCompiles
{
Cloneable instanceOfObject;
Cloneable convertToCloneable(Object object)
{
return instanceOfObject.getClass().cast(object);
}
}
class WhyDoesntThis<T>
{
T instanceOfT;
T convertToT(Object object)
{
//compiler error: Type mismatch:
//cannot convert from capture#1-of ? extends Object to T
return instanceOfT.getClass().cast(object);
}
}
In both cases, the instance can be a subclass of the class that is defined,but that should be okay, because it only has to honor the parent's interface. There might be a more complex example that illustrates why it should fail, but I'm struggling to come up with one. Any thoughts?
{
Cloneable instanceOfObject;
Cloneable convertToCloneable(Object object)
{
return instanceOfObject.getClass().cast(object);
}
}
class WhyDoesntThis<T>
{
T instanceOfT;
T convertToT(Object object)
{
//compiler error: Type mismatch:
//cannot convert from capture#1-of ? extends Object to T
return instanceOfT.getClass().cast(object);
}
}
In both cases, the instance can be a subclass of the class that is defined,but that should be okay, because it only has to honor the parent's interface. There might be a more complex example that illustrates why it should fail, but I'm struggling to come up with one. Any thoughts?