Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ambiguous invocation

 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



Why the method bar invocation (line 1) is ambiguous? If one of the above bar method was commented out, the code would be correct.

It is strange that bar(1, 2, 3) invocation on the one hand boxes arguemnts becoming bar(new Integer[] {1, 2, 3}), on the other hand it lefts them unboxed like bar(new int[] {1, 2, 3}). Why is that?
[ August 18, 2006: Message edited by: Jasiek Motyka ]
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this one.
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because neither of the two methods is more specific than others.
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very useful link ;-)
Thanks you both.
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont understand it either.
Link is ok, but in this case isn't very helpful.

because neither of the two methods is more specific than others.

Yes, that's it, but why?

Why first method isnt' more specific than the second?
Why arguments (1,2,3) cannot be converted to int array conataining those numbers?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find it curious that when given int literals, the compiler doesn't consider a method taking int arguments to be "more specific" than autoboxed Integers. I realize autoboxing (and perhaps var args) might throw a bit of a wrench into the "most specific" concept, but I don't see where this is addressed in the JLS. Has anyone found specific documentation of this?
[ August 18, 2006: Message edited by: marc weber ]
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, that's it, but why?

Why first method isnt' more specific than the second?
Why arguments (1,2,3) cannot be converted to int array conataining those numbers?


A method M is more specific than method N if the every valid argument-list for M is a valid argument list for N buit not vice-versa.

In the current case, we see that every valid argument list for
void bar(int... x)
is valid for
void bar(Integer... x)

And vice versa.

Thus, compiler has no reason to "prefer" one over other, since neither of them is "more specific" than other. Hence it refuses to compile the code.
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method M is more specific than method N if the every valid argument-list for M is a valid argument list for N buit not vice-versa.

In the current case, we see that every valid argument list for
void bar(int... x)
is valid for
void bar(Integer... x)


But this is also true for methods
void bar(int x)
void bar(Integer x).

And more specific method for invocation bar(3) is first method. Why is that?
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lucius Stephienn:
But this is also true for methods
void bar(int x) ....(1)
void bar(Integer x). ....(2)

And more specific method for invocation bar(3) is first method. Why is that?


The JLS doesnot specify anything regarding boxing (nothing which I could find) in this context. (The rules are here)

However, my guess is that the first method is indeed more specific than the second method - because everything which is an argument to the first method is a valid argument to the second method, but not vice-versa : "null" can be an argument to method 2 but not to method 1. (Since in method 2, x is a reference)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic