Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7850

Documentation on primitives and wrappers is misleading

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.4.6
    • None
    • Documentation
    • 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

      Attachments

        1. test1.groovy
          0.3 kB
          Sebb
        2. test2.groovy
          0.1 kB
          Sebb
        3. test3.groovy
          0.1 kB
          Sebb

        Activity

          People

            Unassigned Unassigned
            sebb Sebb
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: