Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!n13g2000vbv.googlegroups.com!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: generics puzzle Date: Mon, 17 Oct 2011 09:12:04 -0700 (PDT) Organization: http://groups.google.com Lines: 97 Message-ID: <45bfae98-a142-469b-9b8b-9aa8a59391f1@n13g2000vbv.googlegroups.com> References: <9g2f24Fi0vU1@mid.individual.net> NNTP-Posting-Host: 193.0.246.21 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1318867924 3792 127.0.0.1 (17 Oct 2011 16:12:04 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 17 Oct 2011 16:12:04 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: n13g2000vbv.googlegroups.com; posting-host=193.0.246.21; posting-account=MGO7qgoAAABvyo26eHVDO00044spH-ws User-Agent: G2/1.0 X-HTTP-Via: 1.1 webwasher (Webwasher 6.8.7.9396) X-Google-Web-Client: true X-Google-Header-Order: ASELNKCHRUV X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8923 On Oct 17, 12:41=A0pm, blm...@myrealbox.com wrote: > I'm having a bit of trouble with generics, and while I've come > up with ways of getting done what I want to get done, I wonder > whether there's some nice solution that isn't occurring to me. > > The basic situation is this: > > I have an abstract generic class (call it GThing) with type > parameter T, which has a method (call it set()) that takes a > parameter of type T and another method (call it modified()) that > returns a result of type T. =A0I also have some classes that extend > GThing and specify its type parameter, and code that is meant > to operate on lists of instances of these various subclasses. > What I would *like* to be able to do is construct a list of > GThing objects and then do to each element of it something > like the following: > > =A0 element.set(element.modified()) > > but the compiler objects to that, which I find reasonable enough, > though I'm not enough of a generics expert to really articulate > what the problem is. =A0(I have a feeling that this "type erasure" > thing, which I understand dimly at best, comes into it. =A0:-)? ) The reason is the ? in the type: the compiler does not know the type. The very moment you declare a List> the type of T is unknown even though it's the same instance. Unknown types cannot be compatible yet you try to do invoke a method with argument type ? with a parameter of type ? and since "? incompatible to ?" it is not allowed. You are basically doing this (list1) but bounding does not help (neither way, list2 and list3); only concrete types are able to ensure type compatibility: import java.util.ArrayList; import java.util.List; public class GenericsProblemRK { public static void main(String[] args) { final List> list1 =3D new ArrayList>(); for (final List l : list1) { l.add(l.get(0)); // error final Object x =3D l.get(0); // OK l.add(x); // error } final List> list2 =3D new ArrayList>(); for (final List l : list2) { l.add(l.get(0)); // error final Object x =3D l.get(0); // OK l.add(x); // error } final List> list3 =3D new ArrayList>(); for (final List l : list3) { l.add(l.get(0)); // error final Object x =3D l.get(0); // OK l.add(x); // error } final List> list4 =3D new ArrayList>(); for (final List l : list4) { l.add(l.get(0)); // OK } } } > One fix is to just introduce a method setFromModified() in GThing, > but that doesn't appeal to me. =A0An ugly-hack fix is to add to > GThing a set() method that takes an Object and typecasts it, > but -- yuck, right? =A0 In your case since apparently you want to update internally why not just introduce public void update() { set(modified()); } This will safely compile. Basically this is what you did with setModified() in the wrapper class. All other approaches will be hacky (i.e. involve explicit casting, using Object parameters or return values etc.). Kind regards robert