Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
2.4.6
-
None
-
None
-
Groovy Version: 2.4.6 JVM: 1.7.0_79 Vendor: Oracle Corporation OS: Mac OS X
Description
The doc [1] has the following example:
test1.groovy
int i m(i) void m(long l) { // This is the method that Java would call, since widening has precedence over unboxing. println "in m(long)" } void m(Integer i) { // This is the method Groovy actually calls, since all primitive references use their wrapper class. println "in m(Integer)" }
Whilst the comment correctly identifies the method which is called by Groovy, it's somewhat misleading, since the code involves both widening and boxing.
The following code shows why:
test2.groovy
int i m(i) void m(int l) { // Groovy calls this method println "in m(int)" } void m(Integer i) { println "in m(Integer)" }
In the code sample above, Groovy does not autowrap the primitive int.
See also the following example:
test3.groovy
int i m(i) void m(long l) { // called by Groovy println "in m(long)" } void m(Long i) { println "in m(Long)" }
This shows that Groovy widens rather than boxes.
So I think the statement "since all primitive references use their wrapper class." is at best misleading, and may be wrong.
[1] http://groovy-lang.org/differences.html#_primitives_and_wrappers