Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Dynamic Casting with Generics: Type Erasure Problems

Reply
Thread Tools

Re: Dynamic Casting with Generics: Type Erasure Problems

 
 
William
Guest
Posts: n/a
 
      03-03-2011
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?
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      03-03-2011
On Mar 3, 2:42*pm, William <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      03-04-2011
William <(E-Mail Removed)> 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.

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      03-04-2011
Andreas Leitgeb <(E-Mail Removed)> wrote:
> William <(E-Mail Removed)> 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.
>

 
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
Re: Dynamic Casting with Generics: Type Erasure Problems William Java 16 03-05-2011 05:28 AM
Dynamic Casting with Generics: Type Erasure Problems William Java 5 03-03-2011 06:22 PM
Generics: struggling against type erasure... z-man Java 8 10-09-2006 07:53 AM
Type erasure bug? Safalra Java 12 08-31-2005 02:23 AM
Java Generics, Type Erasure and Frameworks Sebastian Millies Java 1 10-07-2004 10:10 AM



Advertisments