[
https://issues.apache.org/jira/browse/GROOVY-7850?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15485452#comment-15485452
]
John Wagenleitner commented on GROOVY-7850:
-------------------------------------------
Just wanted to point out that the method selected (boxed vs unboxed) appears to
depend on where the call originates. In the script below the assert in the
{{test()}} method will fail but the call in main will pass.
{code}
class Demo {
String a(int i) { 'int' }
String a(Integer bigI) { 'Integer' }
void test() {
String result = a(42)
assert result == 'Integer'
}
static void main(args) {
String result = new Demo().a(42)
assert result == 'Integer'
new Demo().test()
}
}
{code}
> Documentation on primitives and wrappers is misleading
> ------------------------------------------------------
>
> Key: GROOVY-7850
> URL: https://issues.apache.org/jira/browse/GROOVY-7850
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 2.4.6
> Environment: Groovy Version: 2.4.6 JVM: 1.7.0_79 Vendor: Oracle
> Corporation OS: Mac OS X
> Reporter: Sebb
> Attachments: test1.groovy, test2.groovy, test3.groovy
>
>
> The doc [1] has the following example:
> {code:title=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)"
> }
> {code}
> 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:
> {code:title=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)"
> }
> {code}
> In the code sample above, Groovy does not autowrap the primitive int.
> See also the following example:
> {code:title=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)"
> }
> {code}
> 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
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)