my dog learned polymorphism*
The moose likes Java in General and the fly likes var-args overloading compiler error: ambiguous Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "var-args overloading compiler error: ambiguous" Watch "var-args overloading compiler error: ambiguous" New topic
Author

var-args overloading compiler error: ambiguous

Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I tried a test where I overloaded a function using var-args. One
function expected a list of long values and the other expected a list of
Integers. The compiler complained when I tried to use the Integer
version:

The method widen(long[]) is ambiguous for the type WideningAndBoxing

The line about which the compiler complained is a call to widen() with
three Integer parameters, which is the second function (below).

Why does the compiler see this as ambiguous?

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
IMO : When there is a case of var-args conflict, then widening and Boxing are of same priority. You cannot say that first compiler would try to Widen then Box.

Now in your widen method you have conflict of var-args.

Your call matches widen (Integer....) if Boxed
Your call matches widen (long....) if widened

and hence compiler shows ambiguity.


SCJP 6.0 96%

(Connecting the Dots ....)
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Thanks for your reply. My point is that the compiler had to both unbox and then widen in order to find the ambiguous condition. If it had done neither it would have recognized the method.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

When overloading, (widening + var-args) and (Boxing + var - args) can only be used in a mutually exclusive manner. Not together...


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Your call matches widen (Integer....) if Boxed
Your call matches widen (long....) if widened

I agree that calling widen(100, 200, 300) would be ambiguous. But the offending statement uses Integer - not int. So it would not have to box nor widen to find widen(Integer...is).
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

Interestingly if you have Integer... and Long..., you don't get the error. I don't understand why Integer is getting unboxed and then expanding to long... though.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
FYI, I downloaded the latest JDK (jdk 1.6.0_21) and it does not produce the compiler error. My previous version was 1.6.0 but I don't remember the revision. (I downloaded the latest version for other reasons and was pleasantly surprised to find this issue resolved.)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: var-args overloading compiler error: ambiguous
 
Similar Threads
Overloaded varargs param doubt
Doubt on Method Overloading using Var-args
overloaded var - args
Doubt with compiler error about method matching
Var args ... explanation needed