[ 
https://issues.apache.org/jira/browse/GROOVY-7933?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15485152#comment-15485152
 ] 

Jose Ignacio Acin Pozo edited comment on GROOVY-7933 at 9/12/16 8:12 PM:
-------------------------------------------------------------------------

That would be the expected method resolution. In Java.

Groovy documentation says otherwise, as the method resolution works in 
different way, because primitives are autowrapped:
http://docs.groovy-lang.org/latest/html/documentation/core-object-orientation.html#_primitive_types
http://docs.groovy-lang.org/latest/html/documentation/core-differences-java.html#_primitives_and_wrappers

So in Groovy the order for calling "a(true)" should be something like: 
a(Boolean), a(Object) and last but not least a(boolean).

You can test this behaviour by extending the code:

{code}
public class Demo {

   public String a(boolean a) {
        'boolean was called'
   }

   public String a(Object a) {
        'Object was called'
   }

   public String a(Boolean a) {
        'Boolean was called'
   }

   static void main(args) {
       assert new Demo().a((boolean)true) == 'boolean was called' //pass
       assert new Demo().a(true) == 'Boolean was called'          //fail
   }
}
{code}
I am happy to be corrected, but I have used Groovy for a long time, and learned 
this the hard way. If this is actually a bug, then the documentation is very 
wrong.




was (Author: [email protected]):
That would be the expected method resolution. In Java.

Groovy documentation says otherwise, as the method resolution works in 
different way, because primitives are autowrapped:
http://docs.groovy-lang.org/latest/html/documentation/core-object-orientation.html#_primitive_types
http://docs.groovy-lang.org/latest/html/documentation/core-differences-java.html#_primitives_and_wrappers

So in Groovy the order for calling "a(true)" should be something like: 
a(Boolean), a(Object) and last but not least a(boolean).

You can test this behaviour by extending the code:

public class Demo {

   public String a(boolean a) {
        'boolean was called'
   }

   public String a(Object a) {
        'Object was called'
   }

   public String a(Boolean a) {
        'Boolean was called'
   }

   static void main(args) {
       assert new Demo().a((boolean)true) == 'boolean was called' //pass
       assert new Demo().a(true) == 'Boolean was called'          //fail
   }
}

I am happy to be corrected, but I have used Groovy for a long time, and learned 
this the hard way. If this is actually a bug, then the documentation is very 
wrong.


> Incorrect boxing of boolean primitive types
> -------------------------------------------
>
>                 Key: GROOVY-7933
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7933
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.4.7
>            Reporter: Henri Tremblay
>
> A boolean primitive type seems to be boxed for no apparent reason. See the 
> example below. The problem disappear when using @CompileStatic or if 
> explicitly casting to (boolean).
> {code:java}
> public class Demo {
>    public void a(boolean a){
>        System.out.println("boolean was called");
>    }
>    public void a(Object a){
>        System.out.println("Object was called");
>    }
> }
> class Groovy {
>    static void main(String[] args) {
>        def demo = new Demo()
>        demo.a(true)
>    }
> }
> {code}
> *Output:*
> Object was called



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to