作者tkcn (小安)
看板java
标题Re: [问题] Wrapping/Widening + Var-args
时间Wed Sep 8 22:20:10 2010
※ 引述《bennylu (减肥)》之铭言:
: How about this example
: method(5);
: ..
: void method(int... i) {System.out.print("int...");}
: void method(long... i) {System.out.print("long...");}
: 如果没会错意, 那3个步骤只能告诉我哪些method是符合资格的,
: 但并没有提到当有多个候选人时该如何作选择, 所以也不会提到ambiguous问题,
: 下面这篇文章和我有一样的问题
: http://java.itags.org/java-certification/337/
结论先说在前头,
我的看法跟你连结里 jesperyoung 所回覆的相同,
这确实是一个 compiler 的 bug。
===
在 JLS 15.12 Method Invocation Expressions 中有详细的定义:
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12
在 15.12.2 提到:
If several applicable methods have been identified during one of the three
phases of applicability testing, then the most specific one is chosen.
applicable method 的定义则是:
A method is applicable if it is either applicable by subtyping (§15.12.2.2),
applicable by method invocation conversion (§15.12.2.3),
or it is an applicable variable arity method (§15.12.2.4).
(刚好对应三个阶段。)
注: JLS 对这三个阶段的定义比较详细,
我先前贴的直接将 generics method 忽略掉了。
完整定义请参考 15.12.2.2 ~ 15.12.2.4
在做 method resolution 时,会依序测试那三个阶段,
如果在任何一个阶段中,
只有一个 the most specific method,就可以选定它。
如果有多个 the most specific method,便是 ambiguous。
若一个都没有,则继续下一个阶段。
剩下最後一个问题是,如何找出 most specific method?
当有两个 method m1, m2 皆为 applicable 时,
并且 m1 的每一个 parameter 都是 m2 parameter 的 subtype 时,
我们称 m1 is more specific than m2。
注: 上面为了方便说明,我忽略了 generics 和 varargs,
正确定义请参考 15.12.2.5
让我直接举两个例子:
void foo(int a, long b){} // m1
void foo(long a, int b){} // m2
当我呼叫 foo 并传入两个 int 时,如 foo(0,1),
m1, m2 之间并不存在 more specific 关系,
因此 m1, m2 都是 most specific method,造成 ambiguous。
第二个例子:
void foo(float a, long b){} // m1
void foo(long a, int b){} // m2
在这个例子中,因为 m2 的两个参数都可以 implicitly casting 成 m1 的参数,
因此 m2 is more specific method than m1,
所以 resolution 的结果会是选择 m2。
注: 对於 primitive type 来说,subtype 的定义为:
"被箭头指向的 type" 是 "指向它人的 type" 的 subtype,
即 int 是 byte, short, char 的 subtype。
详细可参考 4.10.1。
8-bits 16-bits 32-bits 64-bits
-----------------------------------------------
byte ──→ short ─┬→ int ──→ long
(signed) │ ┌────┘
char ─┘ ↓
(unsigned) float ─→ double
结论就是,根据 JLS 的定义,
void method(int... i) {System.out.print("int...");}
void method(long... i) {System.out.print("long...");}
method(5) 应该会执行参数为 int... 的 method,
但 compiler 却没有这样做,并且认为这是 ambiguous。
而因为这个 bug 的 priority 是 Vary Low,
所以拖了近六年至今还没被处理掉....XD
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.115.134.17
※ 编辑: tkcn 来自: 59.115.134.17 (09/08 22:20)
1F:→ ogamenewbie:把 int 换成 Integer 也一样吗? 09/08 22:31
2F:→ ogamenewbie:欧, 没事, 我以为原po还是 Integer... vs long... 09/08 22:47
3F:→ tkcn:如果是最後一个例子,换成 Integer 也一样不正确 09/08 22:49
4F:→ ogamenewbie:不正确的意思是 compiler bug 还是 compiler error? 09/08 22:51
5F:→ tkcn:compiler 显示 error,但定义是不会才对,所以也是 bug 09/08 22:55
6F:→ ogamenewbie:可是照定义不就是会走到 pass 3 然後 amb 掉吗? 09/08 23:17
7F:→ ogamenewbie:对不起, 我是 int -> Integer, ... 依旧保留的意思 09/08 23:18
8F:→ tkcn:看不是很懂 @@ 09/08 23:21
9F:→ tkcn:好像看懂了,到phase 3时Integer还是比long more specific, 09/08 23:38
10F:→ tkcn:所以根据定义,会选Interger那method去执行 (但目前不是) 09/08 23:43
11F:→ ogamenewbie:请问一下在 JLS 的 pass 中是哪边有关於 Integer... 09/09 01:02
12F:→ ogamenewbie:要 boxing 且 long 要作型转才会在 pass 3 的情况下. 09/09 01:06
13F:→ ogamenewbie:boxing 会比型转 more specific 的说明阿? @_@ 09/09 01:07
前面推文的时後完全忘记我可以回来修文章 Orz
我要说的范围都在 15.12.2.5 之内,
Integer 比 long more specific 这点应该毫无疑问(非 varargs 情况下),
这个部份在 phase 2 就会完成,根据 15.12.2.3:
For 1<=i<=n, the type of ei, Ai, can be converted by
method invocation conversion (§5.3) to Si.
节录 5.3:
Method invocation contexts allow the use of one of the following:
a boxing conversion (§5.1.7) optionally followed by widening reference
conversion
an unboxing conversion (§5.1.8) optionally followed by a widening
primitive conversion.
而根据 phase 3 定义,并没有任何违反上述规则的条件。
※ 编辑: tkcn 来自: 140.122.183.199 (09/09 09:20)
14F:推 ogamenewbie:不要漏掉...啊. (虽然我自己一直在漏掉 orz 09/09 11:52
15F:→ tkcn:我说 phase 3 就是指加上 varargs 了,定义没冲突 09/09 11:54