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

Closure default values cannot be static constants

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.7.0
    • 1.7.2, 1.6.9, 1.8-beta-1
    • None
    • None

    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.

      Attachments

        Activity

          People

            roshandawrani Roshan Dawrani
            bodiam Erik Pragt
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: