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

Cannot apply function/closure arguments with defaults

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • 1.0
    • 1.1-beta-2
    • None
    • None
    • N/A

    Description

      The following code demonstrates the problem. It is handy to be able to "apply" arguments to a function/closure, but these calls do not properly handle the presence of default arguments.

      def xx =

      { a, b, c = "default" -> println "a $a b $b c $c"; }

      ;
      xx(1, 2, 3);
      xx([1, 2, 3]);
      z = [1, 2, 3];
      xx(z);
      z = [1, 2];
      xx(1, 2);
      xx(z);

      def yy (a, b, c = "default")

      { println "a $a b $b c $c"; };
      yy(1, 2, 3);
      yy([1, 2, 3]);
      z = [1, 2, 3];
      yy(z);
      z = [1, 2];
      yy(1, 2);
      yy(z);

      This would make the following ambiguous:

      def zz (a, b = "default", c = "default") { println "a $a b $b c $c"; }

      ;
      zz([1, 2, 3]);

      There needs to be a way, as with Java varargs, to indicate when an array is "an array of arguments" and when it is "an argument that is an array".

      The current, Java-like approach may not be the best. It would be nice to have an "apply" method on functions/closures that would explicitly take "an array of arguments", much like Scheme/JavaScript:

      def zz (a, b = "default", c = "default")

      { println "a $a b $b c $c"; }

      ;
      zz([1, 2, 3]); // "a [1, 2, 3] b default c default"
      zz.apply([1,2,3]); // "a 1 b 2 c 3"

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            garrow John Redford
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: