Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Old favorite

Reply
Thread Tools

Old favorite

 
 
Torkel Franzen
Guest
Posts: n/a
 
      01-27-2006

I'm looking at that showcase example of generification (from
java.util.Collections):


public static <T extends Object & Comparable<? super T>> T
min(Collection<? extends T> coll)

The reasons for the restriction on the parameter T are clear. But
given that restriction, why the wildcard in the type of coll?
Suppose we define a method

public static <T extends Object & Comparable<? super T>> T
min1(Collection<T> coll)

If T is a subtype of Comparable<S> and a subtype of S, then given
any subtype U of T, U is a subtype of Comparable<S> and a
subtype of S. So what calls go through for min but not for min1?


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      01-27-2006
On 27 Jan 2006 09:53:27 +0100, Torkel Franzen <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>
>The reasons for the restriction on the parameter T are clear. But
>given that restriction, why the wildcard in the type of coll?
>Suppose we define a method


try http://mindprod.com/jgloss/generics.html#SYNTATICSOUP

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
Torkel Franzen
Guest
Posts: n/a
 
      01-27-2006
Roedy Green <(E-Mail Removed) > writes:


> try http://mindprod.com/jgloss/generics.html#SYNTATICSOUP


I don't see any answer to my question.
 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      01-27-2006
Torkel Franzen wrote:
> I'm looking at that showcase example of generification (from
> java.util.Collections):
>
>
> public static <T extends Object & Comparable<? super T>> T
> min(Collection<? extends T> coll)
>
> The reasons for the restriction on the parameter T are clear. But
> given that restriction, why the wildcard in the type of coll?
> Suppose we define a method
>
> public static <T extends Object & Comparable<? super T>> T
> min1(Collection<T> coll)
>
> If T is a subtype of Comparable<S> and a subtype of S, then given
> any subtype U of T, U is a subtype of Comparable<S> and a
> subtype of S. So what calls go through for min but not for min1?


min1 cannot operate on collections of subtypes of T if the actual type
of T is specified explicitly. E.g., if ActualU is a subclass of
ActualT, the compiler doesn't like min1 in this situation:

ArrayList<ActualU> listOfU = new ArrayList<ActualU>();
SomeClass.<ActualT>min(listOfU);
SomeClass.<ActualT>min1(listOfU); // No good.

Here's a more complete example:

import java.util.*;

class Main {

public static void main(String[] args) {

ArrayList<CU> uList = new ArrayList<CU>( );

/* Compiler rejects min1. */
Min.<CT>min(uList);
Min.<CT>min1(uList);
}

}

class CS implements Comparable<CT> {
public int compareTo(CT ct) {
return 0;
}
}

class CT extends CS { }

class CU extends CT { }

class Min {
public static <T extends Object & Comparable<? super T>>
T min(Collection<? extends T> coll) {
return null;
}

public static <T extends Object & Comparable<? super T>>
T min1(Collection<T> coll) {
return null;
}
}
 
Reply With Quote
 
Torkel Franzen
Guest
Posts: n/a
 
      01-27-2006
Jeffrey Schwab <(E-Mail Removed)> writes:

> min1 cannot operate on collections of subtypes of T if the actual type
> of T is specified explicitly.


You're right, that's a point I overlooked. We need to rely on type
inference for min1 to work.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-27-2006
On 27 Jan 2006 13:00:58 +0100, Torkel Franzen <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>> try http://mindprod.com/jgloss/generics.html#SYNTATICSOUP

>
> I don't see any answer to my question.


try it again. I have added some further explanation on the
implications of what I said there.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Torkel Franzen
Guest
Posts: n/a
 
      01-27-2006
Roedy Green <(E-Mail Removed) > writes:

> try it again.


An answer exactly to the point has already been given by Jeffrey
Schwab.

 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      01-27-2006
Jeffrey Schwab wrote:
> Torkel Franzen wrote:
>> I'm looking at that showcase example of generification (from
>> java.util.Collections):
>>
>>
>> public static <T extends Object & Comparable<? super T>> T
>> min(Collection<? extends T> coll)
>>
>> The reasons for the restriction on the parameter T are clear. But
>> given that restriction, why the wildcard in the type of coll?
>> Suppose we define a method
>>
>> public static <T extends Object & Comparable<? super T>> T
>> min1(Collection<T> coll)
>>
>> If T is a subtype of Comparable<S> and a subtype of S, then given
>> any subtype U of T, U is a subtype of Comparable<S> and a
>> subtype of S. So what calls go through for min but not for min1?

>
> min1 cannot operate on collections of subtypes of T if the actual type
> of T is specified explicitly. E.g., if ActualU is a subclass of
> ActualT, the compiler doesn't like min1 in this situation:
>
> ArrayList<ActualU> listOfU = new ArrayList<ActualU>();
> SomeClass.<ActualT>min(listOfU);
> SomeClass.<ActualT>min1(listOfU); // No good.


That's easy to fix:

SomeClass.<ActualU>min1(listOfU); // Good stuff.

It becomes more difficult where the collection type is using wildcards.

>
> Here's a more complete example:
>
> import java.util.*;
>
> class Main {
>
> public static void main(String[] args) {
>
> ArrayList<CU> uList = new ArrayList<CU>( );


If that was:

List<? extends CU> uList = new ArrayList<CU>( );

> /* Compiler rejects min1. */
> Min.<CT>min(uList);


Fine.

> Min.<CT>min1(uList);


Main.java:11: <T>min1(java.util.Collection<T>) in Min cannot be applied
to <CU>(java.util.ArrayList<capture of ? extends CU>)
CU n = Min.<CU>min1(uList);
^

Oops. Okay, we'll try to change the explicit generic argument:

CU n = Min.<? extends CU>min1(uList);

Main.java:11: illegal start of type
CU n = Min.<? extends CU>min1(uList);
^
Main.java:11: > expected
CU n = Min.<? extends CU>min1(uList);
^

I'm not entirely sure why that isn't legal, but apparently it isn't.

The only thing to do, without the Collections-style declaration, is to
remove the explicit generic argument and rely on the capture as the
first error says (can't do this for constructors, btw).

CU n = Min.min1(uList);

> }
>
> }
>
> class CS implements Comparable<CT> {
> public int compareTo(CT ct) {
> return 0;
> }
> }
>
> class CT extends CS { }
>
> class CU extends CT { }
>
> class Min {
> public static <T extends Object & Comparable<? super T>>
> T min(Collection<? extends T> coll) {
> return null;
> }
>
> public static <T extends Object & Comparable<? super T>>
> T min1(Collection<T> coll) {
> return null;
> }
> }


Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new hard disk on old, old pc Deano Computer Support 5 10-21-2005 12:32 PM
old printer and old WP in Windows XP? lu Computer Support 6 01-14-2005 02:18 PM
WTB: Old 8mm or old photo cameras Apkesh Digital Photography 1 12-24-2003 08:57 PM
Old Mountain... Old Machines... Seymore Digital Photography 1 12-03-2003 04:25 AM
old sony fd91 finally dying - old age? lefty Digital Photography 5 10-26-2003 05:53 PM



Advertisments