Old favorite

Discussion in 'Java' started by Torkel Franzen, Jan 27, 2006.

  1. 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?
     
    Torkel Franzen, Jan 27, 2006
    #1
    1. Advertising

  2. Torkel Franzen

    Roedy Green Guest

    On 27 Jan 2006 09:53:27 +0100, Torkel Franzen <>
    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.
     
    Roedy Green, Jan 27, 2006
    #2
    1. Advertising

  3. Torkel Franzen, Jan 27, 2006
    #3
  4. 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;
    }
    }
     
    Jeffrey Schwab, Jan 27, 2006
    #4
  5. Jeffrey Schwab <> 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.
     
    Torkel Franzen, Jan 27, 2006
    #5
  6. Torkel Franzen

    Roedy Green Guest

    On 27 Jan 2006 13:00:58 +0100, Torkel Franzen <>
    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.
     
    Roedy Green, Jan 27, 2006
    #6
  7. Roedy Green <> writes:

    > try it again.


    An answer exactly to the point has already been given by Jeffrey
    Schwab.
     
    Torkel Franzen, Jan 27, 2006
    #7
  8. 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/
     
    Thomas Hawtin, Jan 27, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jensen bredal
    Replies:
    3
    Views:
    362
    darrel
    Mar 31, 2005
  2. clintonG

    Your Favorite RegEx Library?

    clintonG, Oct 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    384
    Frankie
    Oct 8, 2005
  3. Jim Hill
    Replies:
    3
    Views:
    408
    Jim Hill
    Feb 12, 2007
  4. *Prot3anThr3ad*

    old repository for old C++ source code

    *Prot3anThr3ad*, Sep 29, 2006, in forum: C++
    Replies:
    6
    Views:
    384
    *Prot3anThr3ad*
    Oct 2, 2006
  5. John Henry
    Replies:
    24
    Views:
    1,020
    alex23
    May 30, 2008
Loading...

Share This Page