Closed GoogleCodeExporter closed 9 years ago
I hear and understand, but can't agree.
Note that ImmutableList<? extends Whatever> doesn't prevent you from calling
.add(null) either. I don't personally think it's a good idea to do this.
Wildcards are things we using, in method parameters, because we have to, but I
really don't like to see them any more than necessary -- they're way too bulky
and unreadable. The notion of using them only on parameters but never on
return values begins with the JDK and we followed it.
The justification is weak, and it would have to be _very_ strong for us to want
to put two methods in an API that do the exact same thing but for a tweak to a
type. (For example, the case for ListMultimap.asListMap() was stronger than
this but still not strong enough.)
Sometimes you have an ImmutableList<? extends Whatever> whether you wanted to
or not (say, because it was passed to you in a public API). Your workaround is
to throw away the wildcard, at least just for the purpose of using
Ordering.lex().
ImmutableList<Whatever> tmp = ImmutableList.copyOf(list);
As you probably know, this doesn't really copy anything.
Original comment by kevinb@google.com
on 17 Jul 2010 at 4:09
The workaround you mentioned is perfectly fine for current purposes. It may be
worth documenting the workaround in a code comment, probably accompanying the
one pasted in the original post. That way, people who are adamant about using
wildcards have an "official" way out, without cluttering up the doc comments
for "most people".
Thanks for the explanation, and yes, totally understand about not having two
methods that really do the same thing. As regards the "no wildcards in return
types", yes, that is explained in Effective Java 2nd ed, but, my understanding
of it is that it applies shallowly, i.e., only to the top-level type arguments.
Perhaps I misread the book.
Also, thanks for articulating your difference of opinion on the use of
wildcards. At least with the workaround, we can effectively each do our own
thing without getting in the other's way. :-)
Original comment by cky944
on 17 Jul 2010 at 5:46
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:15
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:09
Original issue reported on code.google.com by
cky944
on 16 Jul 2010 at 5:45