groovy
  1. groovy
  2. GROOVY-3351

Curried MethodClosure with default parameters wont work

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 3.0
    • Component/s: groovy-runtime
    • Labels:
      None
    • Number of attachments :
      0

      Description

      As detailed on this thread http://www.nabble.com/Curried-closures-with-default-parameters-td21906130.html
      currying a Methodclosure whose original method has default parameters and assigning it to a metaClass means the default parameters are lost, the workaround is to 'manually' curry the closure

      class Foo {
         static fooWithDefaults( String a1, String a2, String a3 = a2, Map a4 = [:] ) { /*stuff*/ }
         static decorate( Class klass ) {
            klass.metaclass.foo = this.&fooWithDefaults.curry("A")
         }
      }
      
      def f = new Foo()
      f.foo("B") // throws MME
      f.foo("B","C",[:]) // works
      

      workaround

      class Foo {
         static fooWithDefaults( String a1, String a2, String a3 = a2, Map a4 = [:] ) { /*stuff*/ }
         static decorate( Class klass ) {
            klass.metaClass.foo = { String a2, String a3 = a2, Map a4 = [:] ->
               Foo.fooWithDefaults("A",a2,a3,a4)
            }
         }
      }
      

        Activity

        No work has yet been logged on this issue.

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            Andres Almiray
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: