Re: Dynamic Casting with Generics: Type Erasure Problems

Discussion in 'Java' started by William, Mar 3, 2011.

  1. William

    William Guest

    Here's what I don't like about the discussions here, and maybe because I'm not thinking about it clearly. Most errors thrown about type erasure are legitimate, in that you can construct a scenario when it will not work. Forinstance... if I had the method

    public <T extends Object> void testT(Class<T> type)
    {
    Class<T> testClass = Object.class;
    }

    That method will break if I called testT(String.class) because T is now String and Class<String> != Object.class. So that's a run time example of something that would break. In what scenario would my original example not succeed?
     
    William, Mar 3, 2011
    #1
    1. Advertising

  2. William

    Lew Guest

    On Mar 3, 2:42 pm, William <> wrote:
    > Here's what I don't like about the discussions here, and maybe because I'm not thinking about it clearly.  Most errors thrown about type erasure are legitimate, in that you can construct a scenario when it will not work.  For instance... if I had the method    
    >
    > public <T extends Object> void testT(Class<T> type)
    > {
    >      Class<T> testClass = Object.class;
    >
    > }
    >
    > That method will break if I called testT(String.class) because T is now String and Class<String> != Object.class.  So that's a run time example of something that would break.  In what scenario would my original example not succeed?


    Original example:

    class WhyDoesntThis<T>
    {
    T instanceOfT;


    T convertToT(Object object)
    {
    //compiler error: Type mismatch:
    //cannot convert from capture#1-of ? extends Object to T
    return instanceOfT.getClass().cast(object);
    }
    }

    Because there's no way the compiler can know that "capture of ?" is
    cast-compatible with T, it cannot prove that the cast is allowable.

    The compiler only allows casts where one type is a supertype of the
    other. "capture of ?" and "T" do not have that relationship.

    It's not about scenarios, it's about the semantics of the cast
    operator.

    Take a look at the JLS for the details.

    --
    Lew
     
    Lew, Mar 3, 2011
    #2
    1. Advertising

  3. William <> wrote:
    > Yes, there's no way for the compiler to know that
    > ? extends Object can be cast to T.
    > But why doesn't getClass() return ? extends T...


    For any T-typed variable t inside a generic class with
    type-parameter T, t.getClass() would obviously always
    return a Class object that would also hold the promises
    of Class<? extends T>. But, alas, there is some loss of
    information on the way, and the designers seem not to
    have found it worth, yet, to close that gap.

    Now, does the confirmation (that it could indeed have
    been done your way) help you with your current task?

    PS: since your newsreader doesn't place a proper References-
    header, there's a chance that I'll miss any answer from you.
    If you want me to see any answer, either make sure, the
    References-header is there, or Cc: me.
     
    Andreas Leitgeb, Mar 4, 2011
    #3
  4. Andreas Leitgeb <> wrote:
    > William <> wrote:
    >> Yes, there's no way for the compiler to know that
    >> ? extends Object can be cast to T.
    >> But why doesn't getClass() return ? extends T...

    >
    > For any T-typed variable t inside a generic class with
    > type-parameter T, t.getClass() would obviously always
    > return a Class object that would also hold the promises
    > of Class<? extends T>. But, alas, there is some loss of
    > information on the way, and the designers seem not to
    > have found it worth, yet, to close that gap.
    >
    > Now, does the confirmation (that it could indeed have
    > been done your way) help you with your current task?


    Seems like I have to limit that a bit: This is of course
    only true, if the "outer" type (that is used for T) is
    not itself a generic type. If it is, then it might be
    somewhat more complicated, and I'm too lazy now, to think
    it through to the end... Anyway, it is possible that this
    is the actual reason why the mentioned gap is open in the
    first place.

    > PS: since your newsreader doesn't place a proper References-
    > header, there's a chance that I'll miss any answer from you.
    > If you want me to see any answer, either make sure, the
    > References-header is there, or Cc: me.
    >
     
    Andreas Leitgeb, Mar 4, 2011
    #4
    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. Sebastian Millies

    Java Generics, Type Erasure and Frameworks

    Sebastian Millies, Oct 7, 2004, in forum: Java
    Replies:
    1
    Views:
    657
    Chris Uppal
    Oct 7, 2004
  2. z-man
    Replies:
    8
    Views:
    377
    Thomas Weidenfeller
    Oct 9, 2006
  3. Martin Lorentzson

    Generics and type erasure

    Martin Lorentzson, Nov 5, 2006, in forum: Java
    Replies:
    2
    Views:
    422
    Martin Lorentzson
    Nov 6, 2006
  4. William
    Replies:
    5
    Views:
    1,027
  5. William
    Replies:
    16
    Views:
    471
Loading...

Share This Page