Re: Dynamic Casting with Generics: Type Erasure Problems

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

  1. William

    William Guest

    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... just like:

    Number n = 1;
    n.getClass() // returns Class<? extends Number>
    Yes, once it is byte code, the generics are gone, but we're talking about acompiler error. Just as much as this method is purely verified at compiletime

    <T extends Cloneable> void example(T object)
    {
    Cloneable a = object; //assigning ? extends Cloneable to Cloneable
    }

    Now, some people seem to be getting annoyed... I understand that it DOES fail... and I understand that it states as much in the getClass() javadoc... what I'm looking for is WHY DOES IT NEED TO FAIL? (the JLS simply states how it is, offers no explanation of why)

    I guess it boils down to the compiler needing to be smart enough to do typeerasure with only the generics of a class T (like erasing List<String> to List), without erasing the class T itself (as in this example, erasing T toObject) . And I suppose that could be problematic.
    William, Mar 3, 2011
    #1
    1. Advertising

  2. "Peter Duniho" <> wrote in message
    news:...
    > On 3/3/11 12:15 PM, William wrote:
    >>[...]

    >
    > Please stop using Google Groups to post, or figure out how to get your
    > posts to remain connected to the original discussion (i.e. for the
    > "References:" field of your message to retain the message ID of the post
    > to which you're replying).
    >
    > As it is now, every time you reply to a post, you start a new message
    > thread as identified by any normal, well-written news reader. It makes
    > one not care about the message thread altogether.


    It's working fine for me using Windows Live Mail. (No comment on whether
    WLM matches your criteria.)
    Mike Schilling, Mar 4, 2011
    #2
    1. Advertising

  3. Mike Schilling <> wrote:
    >> As it is now, every time you reply to a post, you start a new message
    >> thread as identified by any normal, well-written news reader. It makes
    >> one not care about the message thread altogether.

    > It's working fine for me using Windows Live Mail.


    I'm curious about the details of "fine":
    does WLM merely collect all posts into threads by subject, or
    do you actually see - in the overview - William's posts
    visually connected to the particular post he was answering to?

    Since I have seen properly threaded GG-posts in the past, and see
    William's posts without the References:-header, I'm pressed to believe
    that your definition of "fine" is just more relaxed than Pete's and
    mine.
    Andreas Leitgeb, Mar 4, 2011
    #3
  4. William

    Lars Enderin Guest

    2011-03-04 12:33, Andreas Leipzig skrev:
    > Mike Schilling <> wrote:
    >>> As it is now, every time you reply to a post, you start a new message
    >>> thread as identified by any normal, well-written news reader. It makes
    >>> one not care about the message thread altogether.

    >> It's working fine for me using Windows Live Mail.

    >
    > I'm curious about the details of "fine":
    > does WLM merely collect all posts into threads by subject, or
    > do you actually see - in the overview - William's posts
    > visually connected to the particular post he was answering to?
    >
    > Since I have seen properly threaded GG-posts in the past, and see
    > William's posts without the References:-header, I'm pressed to believe
    > that your definition of "fine" is just more relaxed than Pete's and
    > mine.


    I also don't think that William's posts thread "fine". His latest post has
    In-Reply-To:
    <>

    which happens to refer to Lew's reply to his previous post. William does
    not quote anything and does not include any attribution, and there is no
    References header. Lew also used Google Groups, apparently, but his post
    does have References, attribution, and quote.
    I don't know why Thunderbird does not use In-Reply-To in the absence of
    References, however.
    Lars Enderin, Mar 4, 2011
    #4
  5. On 03/04/2011 07:00 AM, Lars Enderin wrote:
    > I don't know why Thunderbird does not use In-Reply-To in the absence of
    > References, however.


    It does do it, but NNTP servers only send References in the XOVER
    command, so when we thread the message, we fail to see the In-Reply-To
    header.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Mar 4, 2011
    #5
  6. William

    Guest

    On 4 mar, 13:59, Joshua Cranmer <> wrote:
    > On 03/04/2011 07:00 AM, Lars Enderin wrote:
    >
    > > I don't know why Thunderbird does not use In-Reply-To in the absence of
    > > References, however.

    >
    > It does do it, but NNTP servers only send References in the XOVER
    > command, so when we thread the message, we fail to see the In-Reply-To
    > header.


    I see. In theory, Thunderbird could revise the threading once all
    headers are available. As an experiment, I am using GG to view the
    thread and to write this reply. We'll see how the threading works. If
    it works correctly, William must be using a different reply command.
    From his point of view, the message he replies to is visible directly
    above his reply, so he may not be aware that he is communicating on
    Usenet, and does not see any need to quote or attribute anything.
    GG waens me, however, that I am posting to a Usenet group.
    , Mar 4, 2011
    #6
  7. William

    Lew Guest

    William wrote:
    > Now, some people seem to be getting annoyed... I understand that it DOES fail... and I understand that it states as much in
    > the getClass() javadoc... what I'm looking for is WHY DOES IT NEED TO FAIL?  (the JLS simply states how it is, offers no
    > explanation of why)
    >


    That's because "the JLS says so" *is* why. It defines the language,
    not explains it.

    And there's no need to shout. Also, it only takes one period to end a
    sentence.

    --
    Lew
    Lew, Mar 4, 2011
    #7
  8. William

    Lew Guest

    On Mar 3, 3:15 pm, William <> wrote:
    > Yes, there's no way for the compiler to know that ? extends Object can becast to T.  But why doesn't getClass() return ? extends T... just like:
    >
    > Number n = 1;
    > n.getClass() // returns Class<? extends Number>
    > Yes, once it is byte code, the generics are gone, but we're talking abouta compiler error.  
    > Just as much as this method is purely verified at compile time
    >


    I explained upthread why that error occurs. There is not enough
    information at compile time to verify that 'T' and '? extends Object'
    are in a sub/super-type relationship. Simple.

    > <T extends Cloneable> void example(T object)
    > {
    >     Cloneable a = object;  //assigning ? extends Cloneable to Cloneable
    >
    > }
    >
    > Now, some people seem to be getting annoyed... I understand that it DOES fail... and I understand that it states as much in
    > the getClass() javadoc... what I'm looking for is WHY DOES IT NEED TO FAIL?  (the JLS simply states how it is, offers no
    > explanation of why)
    >


    Because 'T' and '? extends Object' do not have a subtype/supertype
    relationship.

    --
    Lew
    Lew, Mar 4, 2011
    #8
  9. William

    Lew Guest

    Lars Enderin wrote:
    > References header. Lew also used Google Groups, apparently, but his post
    > does have References, attribution, and quote.
    > I don't know why Thunderbird does not use In-Reply-To in the absence of
    > References, however.


    I use Thunderbird through a news server sometimes, GG other times.

    It's like a convenience store - GG is a terrible thing except when
    it's convenient.

    --
    Lew
    Lew, Mar 4, 2011
    #9
  10. On 04/03/2011 8:48 AM, wrote:
    > On 4 mar, 13:59, Joshua Cranmer<> wrote:
    >> On 03/04/2011 07:00 AM, Lars Enderin wrote:
    >>
    >>> I don't know why Thunderbird does not use In-Reply-To in the absence of
    >>> References, however.

    >>
    >> It does do it, but NNTP servers only send References in the XOVER
    >> command, so when we thread the message, we fail to see the In-Reply-To
    >> header.

    >
    > I see. In theory, Thunderbird could revise the threading once all
    > headers are available. As an experiment, I am using GG to view the
    > thread and to write this reply. We'll see how the threading works. If
    > it works correctly, William must be using a different reply command.
    > From his point of view, the message he replies to is visible directly
    > above his reply, so he may not be aware that he is communicating on
    > Usenet, and does not see any need to quote or attribute anything.
    > GG waens me, however, that I am posting to a Usenet group.


    Assuming Google won't fix its broken-in-an-ever-changing-way news
    service, there is one thing other newsservers can do to mitigate this
    latest disaster, realistically accepting that Google can't be changed
    nor can everyone be convinced not to use Google:

    Simply this. If XOVER is requested for an article that lacks a
    References: header but has an In-Reply-To: header, send the contents of
    the latter as the References: for the purpose of the XOVER request.

    --
    public final class JSnarker
    extends JComponent
    A JSnarker is an NNTP-aware component that asynchronously provides
    snarky output when the Ego.needsPuncturing() event is fired in cljp.
    javax.swing.JSnarker, Mar 4, 2011
    #10
  11. William

    Lars Enderin Guest

    2011-03-04 19:05, javax.swing.JSnarker skrev:
    > On 04/03/2011 8:48 AM, wrote:
    >> On 4 mar, 13:59, Joshua Cranmer<> wrote:
    >>> On 03/04/2011 07:00 AM, Lars Enderin wrote:
    >>>
    >>>> I don't know why Thunderbird does not use In-Reply-To in the absence of
    >>>> References, however.
    >>>
    >>> It does do it, but NNTP servers only send References in the XOVER
    >>> command, so when we thread the message, we fail to see the In-Reply-To
    >>> header.

    >>
    >> I see. In theory, Thunderbird could revise the threading once all
    >> headers are available. As an experiment, I am using GG to view the
    >> thread and to write this reply. We'll see how the threading works. If
    >> it works correctly, William must be using a different reply command.
    >> From his point of view, the message he replies to is visible directly
    >> above his reply, so he may not be aware that he is communicating on
    >> Usenet, and does not see any need to quote or attribute anything.
    >> GG warns me, however, that I am posting to a Usenet group.

    >
    > Assuming Google won't fix its broken-in-an-ever-changing-way news
    > service, there is one thing other newsservers can do to mitigate this
    > latest disaster, realistically accepting that Google can't be changed
    > nor can everyone be convinced not to use Google:
    >
    > Simply this. If XOVER is requested for an article that lacks a
    > References: header but has an In-Reply-To: header, send the contents of
    > the latter as the References: for the purpose of the XOVER request.


    Why not, but I did not have to do anything special to produce a
    References header in GG. I just hit Reply ("Svar" in Swedish). I don't
    know how William handles replies. He seems to have configured his
    interface to not quote the post he is responding to. What else he's
    doing I cannot know.
    Lars Enderin, Mar 4, 2011
    #11
  12. On 03/04/2011 01:05 PM, javax.swing.JSnarker wrote:
    > Assuming Google won't fix its broken-in-an-ever-changing-way news
    > service, there is one thing other newsservers can do to mitigate this
    > latest disaster, realistically accepting that Google can't be changed
    > nor can everyone be convinced not to use Google:
    >
    > Simply this. If XOVER is requested for an article that lacks a
    > References: header but has an In-Reply-To: header, send the contents of
    > the latter as the References: for the purpose of the XOVER request.


    The easiest way to fix it is on article injection. Since I'm guessing
    Google Groups mangles Message-IDs when it sends out messages-as-emails
    and it appears to be repairing them on reinjection, it should be able to
    easily add on the References. Or, if you want a more brain-dead
    approach, send out the NNTP-as-email with the original Message-ID tacked
    onto the References, so that you can reuse that References on injection
    without having to do any parsing.

    But Google has shown no indication that it is willing to fix anything
    related to Google Groups, which leaves the most palatable response
    option being to convince other news servers to add In-Reply-To to the
    overview format (not likely), or doing article rethreading on message
    display (even less likely), or actually adding in a feature to allow
    users to manually redo threading. But I digress...

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Mar 4, 2011
    #12
  13. On 03/04/2011 01:25 PM, Lars Enderin wrote:
    > Why not, but I did not have to do anything special to produce a
    > References header in GG. I just hit Reply ("Svar" in Swedish). I don't
    > know how William handles replies. He seems to have configured his
    > interface to not quote the post he is responding to. What else he's
    > doing I cannot know.


    Judging by header presence, he appears to be using email to post
    messages. (Normally, GG via the web interface passes along
    X-HTTP-UserAgent, which his post lacks). Whee, yet another broken
    mail-to-news gateway!

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Mar 4, 2011
    #13
  14. William

    Lew Guest

    On Mar 4, 11:39 am, Peter Duniho <> wrote:
    > On 3/4/11 8:11 AM, Lew wrote:
    >
    > > Lars Enderin wrote:
    > >> References header. Lew also used Google Groups, apparently, but his post
    > >> does have References, attribution, and quote.
    > >> I don't know why Thunderbird does not use In-Reply-To in the absence of
    > >> References, however.

    >
    > > I use Thunderbird through a news server sometimes, GG other times.

    >
    > > It's like a convenience store - GG is a terrible thing except when
    > > it's convenient.

    >
    > Heh.
    >
    > Well, maybe you can give William some pointers on how to post using GG
    > without screwing up the threading.  You seem to be able to do that just
    > fine, so it seems like he ought to be able to as well.
    >
    > I almost never use a convenience store, and definitely never use GG, so
    > I can't offer him any specific advice (other than to stop using GG).
    >


    When I want to reply to a post via GG, I click on the little link
    below the post entitled "Reply", type something in, click "Send", and
    GG does the rest.

    It's *super* difficult!

    --
    Lew
    Lew, Mar 4, 2011
    #14
  15. "Andreas Leitgeb" <> wrote in message
    news:...
    > Mike Schilling <> wrote:
    >>> As it is now, every time you reply to a post, you start a new message
    >>> thread as identified by any normal, well-written news reader. It makes
    >>> one not care about the message thread altogether.

    >> It's working fine for me using Windows Live Mail.

    >
    > I'm curious about the details of "fine":
    > does WLM merely collect all posts into threads by subject, or
    > do you actually see - in the overview - William's posts
    > visually connected to the particular post he was answering to?


    Since William doesn't quote, it didn't occur to me he was replying to
    anything in particular.
    Mike Schilling, Mar 5, 2011
    #15
  16. "Lew" <> wrote in message
    news:...
    > William wrote:
    >> Now, some people seem to be getting annoyed... I understand that it DOES
    >> fail... and I understand that it states as much in
    >> the getClass() javadoc... what I'm looking for is WHY DOES IT NEED TO
    >> FAIL? (the JLS simply states how it is, offers no
    >> explanation of why)
    >>

    >
    > That's because "the JLS says so" *is* why. It defines the language,
    > not explains it.


    But if there were no underlying logic, Java would be unusable. No one can
    keep in mind that number of special cases. And generally, when there's a
    limit to what generics can do, there is a good reason for it, e.g. not being
    able to create an array of type T where you can create a List of type T,
    because arrays are reified but Lists are not. "Why can't I do this
    sensible-looking thing?" is quite a reasonable question, because the answer
    will usually teach you something.
    Mike Schilling, Mar 5, 2011
    #16
  17. William

    Lew Guest

    Mike Schilling wrote:
    > Lew wrote :
    >> William wrote:
    >>> Now, some people seem to be getting annoyed... I understand that it DOES
    >>> fail... and I understand that it states as much in
    >>> the getClass() javadoc... what I'm looking for is WHY DOES IT NEED TO FAIL?
    >>> (the JLS simply states how it is, offers no
    >>> explanation of why)
    >>>

    >>
    >> That's because "the JLS says so" *is* why. It defines the language,
    >> not explains it.

    >
    > But if there were no underlying logic, Java would be unusable. No one can keep
    > in mind that number of special cases. And generally, when there's a limit to
    > what generics can do, there is a good reason for it, e.g. not being able to
    > create an array of type T where you can create a List of type T, because
    > arrays are reified but Lists are not. "Why can't I do this sensible-looking
    > thing?" is quite a reasonable question, because the answer will usually teach
    > you something.


    Maybe you noticed that we answered that question separately, and indeed before
    the cited post from the OP.

    The OP raised two issues, again as you may have noticed. One was the question
    why, and the other was the observation that the JLS didn't explain why. I
    answered his post with two posts, each focusing on one of those two issues.

    Do you have a problem with that?

    --
    Lew
    Honi soit qui mal y pense.
    Lew, Mar 5, 2011
    #17
    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:
    649
    Chris Uppal
    Oct 7, 2004
  2. z-man
    Replies:
    8
    Views:
    366
    Thomas Weidenfeller
    Oct 9, 2006
  3. Martin Lorentzson

    Generics and type erasure

    Martin Lorentzson, Nov 5, 2006, in forum: Java
    Replies:
    2
    Views:
    418
    Martin Lorentzson
    Nov 6, 2006
  4. William
    Replies:
    5
    Views:
    1,017
  5. William
    Replies:
    3
    Views:
    647
    Andreas Leitgeb
    Mar 4, 2011
Loading...

Share This Page