Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #8923
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: generics puzzle |
| Date | 2011-10-17 09:12 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <45bfae98-a142-469b-9b8b-9aa8a59391f1@n13g2000vbv.googlegroups.com> (permalink) |
| References | <9g2f24Fi0vU1@mid.individual.net> |
On Oct 17, 12:41 pm, blm...@myrealbox.com <blmblm.myreal...@gmail.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. I 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:
>
> 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. (I have a feeling that this "type erasure"
> thing, which I understand dimly at best, comes into it. :-)? )
The reason is the ? in the type: the compiler does not know the type.
The very moment you declare a List<GThing<?>> 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<List<?>> list1 = new ArrayList<List<?>>();
for (final List<?> l : list1) {
l.add(l.get(0)); // error
final Object x = l.get(0); // OK
l.add(x); // error
}
final List<List<? extends String>> list2 = new ArrayList<List<?
extends String>>();
for (final List<? extends String> l : list2) {
l.add(l.get(0)); // error
final Object x = l.get(0); // OK
l.add(x); // error
}
final List<List<? super String>> list3 = new ArrayList<List<? super
String>>();
for (final List<? super String> l : list3) {
l.add(l.get(0)); // error
final Object x = l.get(0); // OK
l.add(x); // error
}
final List<List<String>> list4 = new ArrayList<List<String>>();
for (final List<String> 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. An ugly-hack fix is to add to
> GThing a set() method that takes an Object and typecasts it,
> but -- yuck, right?
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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-17 10:41 +0000
Re: generics puzzle Steven Simpson <ss@domain.invalid> - 2011-10-17 13:14 +0100
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-17 15:14 +0100
Re: generics puzzle markspace <-@.> - 2011-10-17 07:33 -0700
Re: generics puzzle Steven Simpson <ss@domain.invalid> - 2011-10-17 16:26 +0100
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-18 14:48 +0000
Re: generics puzzle Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-10-17 15:36 +0000
Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-17 08:58 -0700
Re: generics puzzle Steven Simpson <ss@domain.invalid> - 2011-10-18 10:45 +0100
Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-18 09:42 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:25 +0000
Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-19 10:04 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-20 14:14 +0000
Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-20 11:11 -0700
Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-17 09:12 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-18 14:49 +0000
Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-18 18:27 +0200
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-18 17:45 +0000
Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-18 22:15 +0200
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-18 18:59 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:28 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-20 17:21 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:27 +0000
Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-21 20:34 +0200
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-22 18:50 +0000
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-22 21:02 +0100
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-25 07:04 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-25 23:25 -0700
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-26 21:56 +0100
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-27 08:59 +0000
eclipse shortcuts again (was Re: generics puzzle) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-29 17:05 +0000
Re: eclipse shortcuts again (was Re: generics puzzle) Four of Seventeen <fseventeen@gmail.com> - 2011-10-29 19:49 -0700
Re: eclipse shortcuts again (was Re: generics puzzle) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-31 11:17 +0000
Re: eclipse shortcuts again (was Re: generics puzzle) Four of Seventeen <fseventeen@gmail.com> - 2011-10-31 05:39 -0700
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-23 01:30 -0700
Re: generics puzzle Lew <lewbloch@gmail.com> - 2011-10-23 08:56 -0700
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-24 02:46 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-25 07:05 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-25 23:29 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-31 11:14 +0000
Re: generics puzzle Four of Seventeen <fseventeen@gmail.com> - 2011-10-31 05:34 -0700
Re: generics puzzle markspace <-@.> - 2011-10-18 21:21 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:29 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-20 17:22 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:28 +0000
Re: generics puzzle Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-21 06:22 -0300
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:29 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-23 01:20 -0700
Re: generics puzzle Martin Gregorie <martin@address-in-sig.invalid> - 2011-10-23 09:51 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-23 03:28 -0700
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-23 15:59 +0100
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-24 02:46 -0700
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-23 15:55 +0100
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-20 21:00 +0100
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:26 +0000
Re: generics puzzle Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-21 05:57 -0300
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:28 +0000
csiph-web