Re: Generics Frustrations
Re: Generics Frustrations
- Subject: Re: Generics Frustrations
- From: Roger Perryman <email@hidden>
- Date: Tue, 19 Aug 2008 15:43:56 -0400
Thanks to those who offered help. For anyone following this thread,
the solution for creating the dictionary was to define it as
NSDictionary<String, ? extends Object> tmpDict = new
NSDictionary<String, Object>(
new NSArray<Object>( new Object[] { location, schedType,
Integer.valueOf( nWeekday ) } ),
new NSArray<String>( new String[] { "toLocation", "toScheduleType",
"toDay.displayOrder" } ) );
Note that it is using String[] instead of Object[] for the keys.
As for the problem with interactions from EOUtilities, I grudgingly
added the @SupressWarnings ( "unchecked" ) to the method once I had
removed all the other warnings. I couldn't find a way to apply it to
a single line.
Roger
On Aug 15, 2008, at 3:36 AM, Lachlan Deck wrote:
On 15/08/2008, at 12:26 AM, Roger Perryman wrote:
I am converting a project from Xcode 2.x (WO 5.3) to Eclipse 3.4
(WO 5.4.2). I have decided to also "upgrade" to using generics. So
far, it has not been a fun experience.
It takes a while to get used to.
I've read over the threads on this list and several of the
external references provided. Each time I think I have it figured
out and try it on my code, I soon discover that I must be missing
something.
Depends.
In many (but not all) cases, I was able to clear up the errors and
warnings by making everything <?> or <? extends Object>
A sign of the below.
but it just seems wrong to have to explicitly tell it to use
whatever it wants. Below are a few examples of some typical code
snippets that are causing problems. What is the correct way to
write these using generics?
Based on further reading, I know this is not the right way to
write this, but it works and does not generate errors/warnings.
<...>
In theory, I believe this is the "more correct" way to write it
but this generates warnings/errors
NSDictionary<String, ? extends Object> tmpDict = new
NSDictionary<String, ? extends Object>(
new NSArray<? extends Object>( new Object[] { location,
schedType, Integer.valueOf( nWeekday ) } ),
new NSArray<? extends Object>( new Object[] { "toLocation",
"toScheduleType", "toDay.displayOrder" } ) );
Only this part below will give you warnings for which you can
choose @SupressWarnings (via cmd+1)
NSArray<Schedule> schedules = EOUtilities.objectsMatchingValues
( ec, "Schedule", tmpDict );
---------
Here, I get a warning icon in the gutter of the first line telling
me "The expression on type NSArray needs unchecked conversion to
conform to NSArray<Schedule>." I'm sure this is because
EOUtilities don't explicitly return an array of Schedules. But, if
I change it to <?> or <? extends EOGenericRecord> or <? extends
EOCustomRecord>, then the warning goes away
Don't do that. Use the right type above as you know it to be right.
Don't push the problem further down unless you're only needing
<EOEnterpriseObject>.
---------
availabilityByPeriods is a dictionary of dictionaries of strings
{ PERIOD = { "HHMM" = "1"; } }
sKey = "MMDDYYYY_HHMM"
Then it should be declared as such. i.e.,
NSMutableDictionary<String,? extends NSDictionary<String,String>>
---------
This one is even more confusing to me. It looks the same as some
of the snippets above. Yet, in this case, it complains that "the
constructor NSDictionary<String, Object>( NSArray<Object>,
NSArray<Object> ) is undefined."
NSDictionary<String, ? extends Object> tmpDict = new
NSDictionary<String, Object>(
new NSArray<Object>( new Object[] { location, schedType,
Integer.valueOf(nWeekday) } ),
new NSArray<Object>( new Object[] { "toLocation",
"toScheduleType", "toDay.displayOrder" } ) );
You've declared NSDictionary<String, ? extends Object> and you're
passing an NSArray<Object> rather than NSArray<String> for the keys.
with regards,
--
Lachlan Deck
_______________________________________________
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