Re: Generics Frustrations
Re: Generics Frustrations
- Subject: Re: Generics Frustrations
- From: Roger Perryman <email@hidden>
- Date: Thu, 14 Aug 2008 14:20:09 -0400
I would prefer to use generics to make my code more robust. Generics
have revealed a few cases where I had bugs/quirks in my code that the
compiler never complained about before. In most cases, it is tedious
but easy to get generics working. The places where I seem to
encounter the most difficulties is when interacting with EOF, for
example building dictionaries to use as arguments, calling
EOUtilities methods, and sometimes getting the results from
objectsWithFetchSpecification().
Roger
On Aug 14, 2008, at 11:23 AM, Ramsey Gurley wrote:
Hi Roger,
Do you want to use generics or are you just trying to make the
warnings go away? If it's the latter, adding
@SuppressWarnings("unchecked")
public class MyClass {
...
}
does the trick for me. It works with methods and members too if
you don't want to suppress on the entire class file. If you want
to ignore them everywhere without editing, you can turn off
generics warnings in Eclipse preferences "Java->Compiler->Errors/
Warnings->Generic Types". But I'm using Eclipse 3.3.x and WO
5.4.2 right now, so the solution may be different for you.
I'm not sure you'll want to upgrade to 3.4 just yet though. I'm
under the impression that Eclipse 3.4 is bleeding edge and not
necessarily a good idea if you need a "stable" version. In any
case, I don't think Eclipse 3.4 works with any other version of
WOLips except the nightly builds right now. Make sure you aren't
trying to use the wrong version of WOLips.
rg
On Aug 14, 2008, at 10:26 AM, Roger Perryman wrote:
All,
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. 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. In many (but not all)
cases, I was able to clear up the errors and warnings by making
everything <?> or <? extends Object> 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.
NSDictionary<Object, Object> tmpDict = new NSDictionary<Object,
Object>(
new NSArray<Object>( new Object[] { location, schedType,
Integer.valueOf( nWeekday) } ),
new NSArray<Object>( new Object[] { "toLocation",
"toScheduleType", "toDay.displayOrder" } ) );
NSArray<?> schedules = EOUtilities.objectsMatchingValues( ec,
"Schedule", tmpDict );
---------
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" } ) );
NSArray<Schedule> schedules = EOUtilities.objectsMatchingValues
( ec, "Schedule", tmpDict );
One problem with this approach is that, while the keys are, for
all intents and purposes, Strings, you have to declare the array
as <Object> because it is created with "new Object[] { entries }."
---------
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 and the next line
generates an error icon and complains about the signature of
showSchedules(). Again, when I alter that, it cascades like dominoes.
NSArray<Schedule> schedules = EOUtilities.objectsForEntityNamed
( ec, "Schedule" );
showSchedules( schedules );
public static void showSchedules( NSArray<Schedule> schedules );
---------
availabilityByPeriods is a dictionary of dictionaries of strings
{ PERIOD = { "HHMM" = "1"; } }
sKey = "MMDDYYYY_HHMM"
public static NSMutableDictionary<String, ? extends Object>
arrangeTimeSlotsByPeriod( NSMutableDictionary<String, ? extends
Object> availabilityByPeriods, String sKey )
{
NSMutableDictionary<String, ? extends Object> dateDict = new
NSMutableDictionary<String, Object>();
NSMutableDictionary<String, ? extends Object> periodDict = new
NSMutableDictionary<String, Object>();
String sDate = sKey.substring( 0, 8 );
String sTime = sKey.substring( 9 );
int nPeriod = TimePeriod.timePeriodForKey
( sKey );
if ( availabilityByPeriods.objectForKey( sDate ) == null )
availabilityByPeriods.setObjectForKey( dateDict, sDate );
// Type safety: Unchecked cast from capture#40-of ? extends Object
to NSMutableDictionary<String, ? extends Object>
dateDict = (NSMutableDictionary<String, ? extends Object>)
availabilityByPeriods.objectForKey( sDate );
if ( dateDict.objectForKey( TimePeriod.dayPeriods.objectAtIndex
( nPeriod ) ) == null )
dateDict.setObjectForKey( periodDict,
TimePeriod.dayPeriods.objectAtIndex( nPeriod ) );
// Type safety: Unchecked cast from capture#45-of ? extends Object
to NSMutableDictionary<String, ? extends Object>
periodDict = (NSMutableDictionary<String, ? extends Object>)
dateDict.objectForKey( TimePeriod.dayPeriods.objectAtIndex
( nPeriod ) );
periodDict.setObjectForKey( "1", sTime ); // Value is unimportant
return availabilityByPeriods;
}
How can I find "capture #x of type expression" as referenced in
the error messages?
---------
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" } ) );
Roger
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40mac.com
This email sent to email@hidden
_______________________________________________
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