• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Generics Frustrations
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Generics Frustrations


  • Subject: Re: Generics Frustrations
  • From: "John Huss" <email@hidden>
  • Date: Thu, 14 Aug 2008 10:20:43 -0500

Personally, I think generics are a waste of time except as a means of documenting method parameters and return values in library code.

And in cases where want Object anyway, just skip the generics.  But that's just me.

John

On Thu, Aug 14, 2008 at 9:26 AM, Roger Perryman <email@hidden> 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:

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

References: 
 >Re: Generics Frustrations (From: Roger Perryman <email@hidden>)

  • Prev by Date: Re: Generics Frustrations
  • Next by Date: Re: Generics Frustrations
  • Previous by thread: Re: Generics Frustrations
  • Next by thread: Re: Generics Frustrations
  • Index(es):
    • Date
    • Thread