groovy
  1. groovy
  2. GROOVY-4134

Closure default values cannot be static constants

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.7.0
    • Fix Version/s: 1.7.2, 1.6.9, 1.8-beta-1
    • Component/s: None
    • Labels:
      None
    • Testcase included:
      yes
    • Number of attachments :
      0

      Description

      I'd like to use a default value in a closure argument, but at the moment,
      it's not working, and I was wondering if the thing I'm trying is not
      supported...

      This is my code:

      Sheet.metaClass.dump =

      { Integer maxRows = Integer.MAX_VALUE -> }

      Code from Tim Yates:

      This works:

      def max = Integer.MAX_VALUE
      String.metaClass.dump = { Integer maxRows = max ->
      delegate.substring( 0, delegate.length() < maxRows ? delegate.length() : maxRows )
      }
      assert 'hello'.dump( 2 ) == 'he'
      assert 'hello'.dump() == 'hello'

      but as you say, putting the Integer.MAX_VALUE in the closure definition throws the MissingPropertyException.

        Activity

        Hide
        Tim Yates added a comment -

        Seems to be similar to GROOVY-1347 but for closures rather than methods

        Show
        Tim Yates added a comment - Seems to be similar to GROOVY-1347 but for closures rather than methods
        Roshan Dawrani made changes -
        Field Original Value New Value
        Assignee Roshan Dawrani [ roshandawrani ]
        Hide
        blackdrag blackdrag added a comment -

        the example works on trunk... somehow this looks familiar... haven't we fixed that already?

        Show
        blackdrag blackdrag added a comment - the example works on trunk... somehow this looks familiar... haven't we fixed that already?
        Hide
        Roshan Dawrani added a comment -

        I just fixed it on trunk. Builds on 1.7.x and 1.6.x have not completed yet.

        Show
        Roshan Dawrani added a comment - I just fixed it on trunk. Builds on 1.7.x and 1.6.x have not completed yet.
        Hide
        blackdrag blackdrag added a comment -

        but it worked here without your fix... strange

        Show
        blackdrag blackdrag added a comment - but it worked here without your fix... strange
        Hide
        Roshan Dawrani added a comment -

        I hope you are talking about the first part of the JIRA code here. 2nd part is the workaround Tim suggested.

        Anyway, I first reproduced the error and then fixed it.

        Show
        Roshan Dawrani added a comment - I hope you are talking about the first part of the JIRA code here. 2nd part is the workaround Tim suggested. Anyway, I first reproduced the error and then fixed it.
        Hide
        Roshan Dawrani added a comment -

        Below is what should fail without my current fix (as it does on http://groovyconsole.appspot.com/):

        class Sheet{}
        
        Sheet.metaClass.dump = { Integer maxRows = Integer.MAX_VALUE ->  }
        
        new Sheet().dump()
        
        Show
        Roshan Dawrani added a comment - Below is what should fail without my current fix (as it does on http://groovyconsole.appspot.com/): class Sheet{} Sheet.metaClass.dump = { Integer maxRows = Integer .MAX_VALUE -> } new Sheet().dump()
        Hide
        blackdrag blackdrag added a comment -

        ah, sorry for the confusion, I had a bug in my test, that made it work.. lol

        Show
        blackdrag blackdrag added a comment - ah, sorry for the confusion, I had a bug in my test, that made it work.. lol
        Hide
        Roshan Dawrani added a comment -

        Fixed

        Show
        Roshan Dawrani added a comment - Fixed
        Roshan Dawrani made changes -
        Fix Version/s 1.8-beta-1 [ 16013 ]
        Fix Version/s 1.7.2 [ 16237 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 1.6.9 [ 16236 ]
        Resolution Fixed [ 1 ]
        Hide
        Paul King added a comment -

        I haven't looked at your fix yet. Does it help with GROOVY-3278?

        Show
        Paul King added a comment - I haven't looked at your fix yet. Does it help with GROOVY-3278 ?
        Hide
        Paul King added a comment -

        OK, just looking, fix only applies to closures. Feel free to look at GROOVY-3278 if you have time.

        Show
        Paul King added a comment - OK, just looking, fix only applies to closures. Feel free to look at GROOVY-3278 if you have time.
        Hide
        Roshan Dawrani added a comment -

        I hardly see any overlap between the two issues. So, I guess not - my fix deals just with initial values of closure parameters and has nothing to do with annotations.

        Show
        Roshan Dawrani added a comment - I hardly see any overlap between the two issues. So, I guess not - my fix deals just with initial values of closure parameters and has nothing to do with annotations.
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Roshan Dawrani
            Reporter:
            Erik Pragt
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: