kgislsompo / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
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

Original comment by fry@google.com on 26 Jan 2011 at 9:49

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 6:18

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 16 Feb 2012 at 7:17

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 2 Mar 2012 at 6:51

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 11 May 2012 at 8:55

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 May 2012 at 7:51

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 22 Jun 2012 at 6:16

GoogleCodeExporter commented 9 years ago
Java 8 introduces some new weirdness with the varargs Lists.newArrayList(). If 
you call Lists.newArrayList(null), then javac7 would resolve the varargs 
method. javac8 reports an error:

error: reference to newArrayList is ambiguous
    Lists.newArrayList(null);
         ^
  both method <E#1>newArrayList(Iterable<? extends E#1>) in Lists and method <E#2>newArrayList(Iterator<? extends E#2>) in Lists match
  where E#1,E#2 are type-variables:
    E#1 extends Object declared in method <E#1>newArrayList(Iterable<? extends E#1>)
    E#2 extends Object declared in method <E#2>newArrayList(Iterator<? extends E#2>)

Original comment by cus...@google.com on 21 Mar 2014 at 4:34

GoogleCodeExporter commented 9 years ago
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:16

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:19

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:10