iDev24 / google-collections

Automatically exported from code.google.com/p/google-collections
0 stars 0 forks source link

Caution about ambiguity in overloaded varargs methods #113

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

given a class:

class Foo implements Iterable<Foo>

then:

Iterable<Foo> i = Lists.newLinkedList(new Foo())

is ambiguous and easily introduces bugs

Original issue reported on code.google.com by jvdne...@gmail.com on 5 Jan 2009 at 4:20

GoogleCodeExporter commented 9 years ago
quibble: I don't think it should introduce bugs since it should fail to 
compile. It
fails to compile, right?

Note: we hate this too, so the new APIs for ImmutableList etc. use different 
method
names (of/copyOf).  Also we're removing the varargs overloads for all the 
collection
factory methods except newArrayList() and newHashSet() and if we were smart we'd
probably remove them all.

Original comment by kevin...@gmail.com on 5 Jan 2009 at 4:34

GoogleCodeExporter commented 9 years ago

The code attached compiles in eclipse, but the test fails.
(The tree iterator (walk) would imho also be a nice addition to the library)

Original comment by jvdne...@gmail.com on 6 Jan 2009 at 12:27

Attachments:

GoogleCodeExporter commented 9 years ago
In this code, you're using raw types and suppressing warnings. I don't claim 
anything
will work right if you do this. Please provide an example that is warning-free 
if one
exists, and please don't make me hunt down a third-party library to compile it. 
Thanks.

Original comment by kevin...@gmail.com on 6 Jan 2009 at 4:56

GoogleCodeExporter commented 9 years ago
Sorry. You're right.
Attached is an example that illustrates the problem properly (although I get 
the 
feeling we're already spending more time on it than it's worth).
I'm using Lists.newArrayList, because Lists.newLinkedList appears to be safe.
(sorry about that as well, 
http://mvnrepository.com/artifact/com.google.collections/google-collections/0.8 
is 
unexpectedly not the same as google-collect-snapshot-20080820.zip from this 
project 
site)
I'm merely trying to say that I didn't get any warnings or errors with this 
code.

Original comment by jvdne...@gmail.com on 6 Jan 2009 at 5:59

Attachments:

GoogleCodeExporter commented 9 years ago
thank you, this example is much better.  Clearly, a class Foo that implements
Iterable<Foo> is going to cause problems for these methods.

I'm reopening this issue to consider removing the varargs newArrayList and 
newHashSet
methods.  They really are not that useful.  However, we may also just choose to 
put a
small warning about this situation in their javadoc.  I lean toward the former.

Original comment by kevin...@gmail.com on 6 Jan 2009 at 6:09

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Mar 2009 at 6:37

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:59

GoogleCodeExporter commented 9 years ago
This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09