Re: Generics and Filtering on a Subclass
Re: Generics and Filtering on a Subclass
- Subject: Re: Generics and Filtering on a Subclass
- From: David Avendasora <email@hidden>
- Date: Mon, 23 Jun 2008 11:52:57 -0400
On Jun 23, 2008, at 11:27 AM, Mike Schrag wrote: Well, this works, but seems like I'm reinventing the wheel.
NSMutableArray<Finished> descendantFinisheds = new NSMutableArray<Finished>();for (Part aPart : descendantParts()) { if (aPart instanceof Finished) { descendantFinisheds.addObject((Finished) aPart); } }
And if I can force the casting of one object, why can't I force the casting of the Array using (NSArray<Finished>) EOQual...?
Filtering with a qualifier is a generic (uh .. not generic like generics) implementation of this filtering, so it's impossible to enforce the rule you're specifying statically. You are going to have to cast somewhere. In your case, you're casting the objects inside the array, but you should be able to cast the resultant array from the original filter also. You may need to specify your original array as NSMutableArray<? extends Part> instead of NSMutableArray<Part>, but regardless you're going to have to cast somewhere in the chain to do this.
Here's my original code, which gives me a compile error (not warning):
public NSArray<Finished> descendantFinisheds() { NSMutableArray<Object> args = new NSMutableArray<Object>(); args.addObject(_PartType.fetchOneFinished(editingContext())); EOQualifier qual = EOQualifier.qualifierWithQualifierFormat("partType = %@", args); NSArray<Finished> descendantFinisheds = EOQualifier.filteredArrayWithQualifier(descendantParts(), qual); return descendantFinisheds; }
It errors on the EOQualifier.filteredArrayWithQualifier(descendantParts(), qual) stating "Type mismatch: cannot convert from NSArray<Part> to NSArray<Finished>"
If I try casting the results of the filteredArrayWithQualifier like so:
NSArray<Finished> descendantFinisheds = (NSArray<Finished>) EOQualifier.filteredArrayWithQualifier(descendantParts(), qual);
I get the same Type mismatch compiler error.
I tried using <? extends Part> instead in the method signature, but later when I use the results of this method, I must have a Finished because I call methods that only a Finished has.
Grr..
Dave
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden