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

Inner classes - Constructor calls with named arguments don't get implicit this reference passed as 1st argument

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.7.1, 1.8-beta-1
    • None
    • None

    Description

      Groovy doesn't yet support setting properties on (non-static) inner classes using named arguments, as in code below

      class MyOuterClass {
          def foo() {
              def ic = new MyInnerClass(fName: 'Roshan', lName: 'Dawrani')
          }
          class MyInnerClass {
              def fName
              def lName
          }
      }
      def oc = new MyOuterClass()
      oc.foo()
      

      The code above fails with the message

      failed to invoke constructor: public MyOuterClass$MyInnerClass(MyOuterClass) with arguments: [] reason: java.lang.IllegalArgumentException: wrong number of arguments
      

      indicating that the implicit "this" reference is not being passed as the first argument.

      Supporting above usage may require significant changes possibly.

      The bit related to "this" not being passed implicitly can be corrected in the meantime - so that at least the following works fine

      class MyOuterClass {
          def foo() {
              def ic = new MyInnerClass(fName: 'Roshan', lName: 'Dawrani')
          }
          class MyInnerClass {
              Map propMap
              def MyInnerClass(Map propMap) {
                  this.propMap = propMap
              }
          }
      }
      def oc = new MyOuterClass()
      oc.foo()
      

      Attachments

        Activity

          People

            roshandawrani Roshan Dawrani
            roshandawrani Roshan Dawrani
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: