From harmony-dev-return-7331-apmail-incubator-harmony-dev-archive=incubator.apache.org@incubator.apache.org Thu May 11 09:30:12 2006 Return-Path: Delivered-To: apmail-incubator-harmony-dev-archive@www.apache.org Received: (qmail 89405 invoked from network); 11 May 2006 09:30:10 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 11 May 2006 09:30:10 -0000 Received: (qmail 85057 invoked by uid 500); 11 May 2006 09:30:05 -0000 Delivered-To: apmail-incubator-harmony-dev-archive@incubator.apache.org Received: (qmail 85016 invoked by uid 500); 11 May 2006 09:30:05 -0000 Mailing-List: contact harmony-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: harmony-dev@incubator.apache.org Delivered-To: mailing list harmony-dev@incubator.apache.org Received: (qmail 85004 invoked by uid 99); 11 May 2006 09:30:05 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 11 May 2006 02:30:05 -0700 X-ASF-Spam-Status: No, hits=1.4 required=10.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (asf.osuosl.org: 82.138.226.220 is neither permitted nor denied by domain of t.p.ellison@gmail.com) Received: from [82.138.226.220] (HELO dublin.purplecloud.com) (82.138.226.220) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 11 May 2006 02:30:04 -0700 Received: (qmail 23953 invoked from network); 11 May 2006 10:29:46 +0100 Received: from unknown (HELO ?192.168.0.2?) (85.133.120.161) by smtp-dublin.purplecloud.com with (DHE-RSA-AES256-SHA encrypted) SMTP; 11 May 2006 10:29:46 +0100 Message-ID: <44630402.3010601@gmail.com> Date: Thu, 11 May 2006 10:29:38 +0100 From: Tim Ellison User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) MIME-Version: 1.0 To: harmony-dev@incubator.apache.org Subject: [classlib] generics puzzler (was: Re: Thanks Stepan! (was: Re: [jira] Resolved: (HARMONY-454) [classlib][luni] java.util.Set generics uplift and related changes)) References: <4461D57E.4070207@gmail.com> <002601c674b4$c40d0240$0e01a8c0@LITTLEGUY> In-Reply-To: X-Enigmail-Version: 0.94.0.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N But the question remains in my mind whether there is any generic type definition you could write that would allow you to cast the entrySet() to a "Set>" equivalent? To put it another way, I would expect that for (Map.Entry entry : map.entrySet()) should complain that the declaration of 'entry' is not compatible with the 'capture-of ? extends K' etc. (It's still a useful cheat though) Regards, Tim Neil Bartlett wrote: > Nathan, > > It's a tricky one to be sure. > > The problem is that the entrySet() method is returning a > "Set>", > which is incompatible with the type "Set extends V>>". It's easier to describe why if I drop the "extends K" > and "extends V" part. So we have "Set" and > "Set>". > > The first one, "Set>" is a set of Map.Entries of > different types - ie it is a heterogeneous collection. It could > contain a Map.Entry and a Map.Entry> > and any other pair of types, all in the same set. > > On the other hand, "Set>" is a > homogenous collection of the same (albeit unknown) pair of types. Eg > it might be a Set>, so all of the entries in the > set MUST be Map.Entry. > > In general, even if you could assign this to a local variable without > unchecked type safety warnings, you wouldn't be able to call any > methods on it because the compiler cannot check whether the access is > type-safe. For example you would not be able to call add() because the > Set must contain a specific type, but the compiler doesn't know which > specific type that is! > > Regards > Neil > > On 5/11/06, Nathan Beyer wrote: >> Does someone understand why this works this way? This seems so odd. I >> know >> there are quirks to the generics syntax, but this in an edge I haven't >> run >> into yet. I haven't been able to make this one click in my head yet. >> >> This compiles fine: >> public synchronized void putAll(Map >> map) { >> for (Map.Entry entry : map.entrySet()) { >> ... >> } >> } >> >> This won't compile at all: >> public synchronized void putAll(Map >> map) { >> Set> entries = >> map.entrySet(); >> ... >> } >> The error is: Type mismatch: cannot convert from >> Set> extends K,capture-of ? extends V>> to Set> V>> >> The suggested quick fix in Eclipse is "Set entries = ...". >> >> This reports a warning: >> public synchronized void putAll(Map >> map) { >> Map map2 = (Map)map; >> } >> The warning is: Type safety: The cast from Map> K,capture-of ? extends V> to Map is actually checking against the >> erased type Map >> The suggested quick fix in Eclipse is to the annotation: >> @SuppressWarnings("unchecked"). >> >> -Nathan >> >> >> > -----Original Message----- >> > From: Tim Ellison [mailto:t.p.ellison@gmail.com] >> > Sent: Wednesday, May 10, 2006 6:59 AM >> > To: harmony-dev@incubator.apache.org >> > Subject: Thanks Stepan! (was: Re: [jira] Resolved: (HARMONY-454) >> > [classlib][luni] java.util.Set generics uplift and related changes) >> > >> > Stepan Mishura (JIRA) wrote: >> > >> > > 2) To avoid casting while-loop was replaced with for-loop. Could you >> > review the change? >> > >> > I was scratching my head about this cast, so I was very pleased to see >> > your elegant solution. >> > >> > I must admit that I don't really understand why the for-loop version is >> > inherently different (other than it 'hides' the casting) -- but I've >> > learned a new pattern there :-) >> > >> > Regards, >> > Tim >> > >> > -- >> > >> > Tim Ellison (t.p.ellison@gmail.com) >> > IBM Java technology centre, UK. >> > >> > >> > --------------------------------------------------------------------- >> > Terms of use : http://incubator.apache.org/harmony/mailing.html >> > To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org >> > For additional commands, e-mail: harmony-dev-help@incubator.apache.org >> >> >> --------------------------------------------------------------------- >> Terms of use : http://incubator.apache.org/harmony/mailing.html >> To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org >> For additional commands, e-mail: harmony-dev-help@incubator.apache.org >> >> > > -- Tim Ellison (t.p.ellison@gmail.com) IBM Java technology centre, UK. --------------------------------------------------------------------- Terms of use : http://incubator.apache.org/harmony/mailing.html To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org For additional commands, e-mail: harmony-dev-help@incubator.apache.org