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
        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
        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.

          People

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

            Dates

            • Created:
              Updated:
              Resolved: