On Thu, 17 Jul 2008, Jason Cavett wrote:
> So, here's a weird problem I ran into using generics (see my other
> post - http://groups.google.com/group/comp....9fff0ed09efd6#)
>
> The issue revolves around general use of a Generics class. For
> example, I have a List of Generic objects that I need to iterate
> through. So...something like this...
>
> // generics class
> public abstract class ErrorRule <T extends DataObject> {
> public abstract boolean verify(T object);
> }
>
> // suite to run the error rules; each DataObject has its own suite
> public abstract class ErrorSuite {
> public boolean verifyAllRules(DataObject object) {
> // rules defined in the concrete class
> // this throws the following warning, though...
> // ErrorRule is a raw type. References to generic type
> ErrorRule<T> should be parameterized
> for(ErrorRule rule : this.rules) {
> rule.verify(object);
> }
> }
> }
>
> So, if I paramaterize it with <? extends DataModel> then the
> "rule.verify()" gets the following error...
> The method verify(capture#2-of ? extends DataObject) in the type
> ErrorRule<capture#2-of ? extends DataObject> is not applicable for the
> arguments (DataObject)
>
> Whoa...any help with that error? Any way to fix this (without doing
> @SuppressWarnings)?
As i think has been explained elsewhere in the thread, what you're trying
to do is basically Wrong.
If you have some specific kind of DataObject, and a collection of
ErrorRules for some specific kind of DataObject, then you can't just apply
one to the other unless the compiler can prove that the specific kinds of
DataObject in question are the same. That means having some chain of
relationships between type variables connecting the two.
The solution is probably to parameterise ErrorSuite with a type variable
<T extends DataObject>, and then write it to only takes ErrorRule<T> as a
rule and T as a subject. Or perhaps ErrorRule<? super T>, i'm not sure.
The trouble there is that code which uses the ErrorSuite then has to be
aware of the type variable to which it is bound, which might be a pain.
There's no really typesafe solution to this - you *have* to preserve a
type-variable link between the ErrorRules and the the subjects.
tom
--
Sometimes it takes a madman like Iggy Pop before you can SEE the logic
really working.