public WORequest createRequest( String method, String aurl, String anHttpVersion, Map< String, ? extends List< String >> someHeaders, NSData content, Map< String, Object > someInfo ) { Map< String, List< String >> correctHeaders = new HashMap< String, List< String > >(); NSList<String> nslist = null; someHeaders.put( "foo", nslist ); // compile error correctHeaders.put( "foo", nslist ); // no problems
NSMutableArray<String> array = null; someHeaders.put( "foo", array ); // compile error correctHeaders.put( "foo", array ); // no problems }
You don't need extends for interfaces AFAIK.
Not sure what you're showing here, but you are correct that you don't need extends to control the value you put into the map. You need the extends to control what generic impls you can pass in as the PARAMETER to the method. So if you're showing that a Map<String, List<String>> allows you to put a subtype of List as the value, I totally agree. The problem is that a Map<String, NSArray<String>> is NOT assignable to a Map<String, List<String>> -- this is the Integer vs Number problem*, and it's not a typesafe operation. In your example, map a Map<String, NSArray<String>> and try to pass that into createRequest -- it will work, but you can't insert into it, because you have no idea precisely what subtype of List the generic was actually written for -- the "? extends" screws your ability to insert into it in a typesafe way. If you remove the "? extends" you can put any list subtype into the value, but you can't pass in a generic that is declared as anything except PRECISELY <String, List<String>>, which causes the problem below: If the method sig changes, it's annoying to people passing values in,
Map< String, NSArray< String >> someHeaders = null; Map< String, Object > someInfo = null; createRequest( "foo", "url", "1", someHeaders, null, someInfo ); No compile errors. I'm not convinced that fixing the method signature would break anything for anyone at all.
public static void doSomething() { Map<String, LinkedList<String>> testMap = new HashMap<String, LinkedList<String>>(); doSomethingElse(testMap); }
public static void doSomethingElse(Map<String, List<String>> test) {
}
this is a compile error .. if the method signature removes the ? extends List you can't pass in a generic that has a V that is explicitly declared as a subtype of List, you can only pass in a V that is declared as List.
ms
* I keep this in my Stickies in like 24pt type, so every time I say "WTF CAN'T I DO THAT", i can run stickies and remind myself:
WHY IT'S ILLEGAL TO UPCAST GENERICS
Map<String, Integer> integers = new HashMap<String, Integer>(); Map<String, Number> numbers = integers; // illegal numbers.put("str", Long.valueOf(1)); // and this is why
now a long is in integers |