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

Non-Map-Based Named Parameter Support

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • None

    Description

      • Groovy should support named parameters as available in many other languages:
        https://en.wikipedia.org/wiki/Named_parameter
      • The suggested syntax and semantics are the ones used by the Kotlin programming language
        https://kotlinlang.org/docs/reference/functions.html#named-arguments
        since
        1. using the assignment operator to define named parameter values
          • Seems a very organic choice in the Java family of languages
          • Differentiates the syntax clearly from the alternate/existing Groovy map based named parameter support
        2. The design decisions are relatively straightforward
        3. Kotlin syntax is close to Groovy (apart from some odd deviations), so what works there should work in Groovy
      • Features:
        • Parameters are assigned a value prioritized in the following order:
          1. positional value
          2. named parameter value
          3. default value
        • Parameters which do not have a default value must be given when calling the method
        • All non-named parameters must come before named-parameters
        • Named parameters can be given in any order
      • Example:
        void foo(final x0, final x1, final x2 = defVal2, final x3 = defVal3, final x4 = defVal4) { /* ... */ }
        // x0 = val0, x1 = val1, x2 = val2, x3 = defVal3, x4 = val4 
        foo(val0, x2 = val2, x4 = val4, x1 = val1) 
        
      • Arguments for using assignment (x=123) over map syntax (x:123) for named parameter value assignment:
        1. Map syntax would collide with a ctor which takes a map as its argument, if the syntax is going to be mapped to a map
        2. Assignment operator is better to read/clearer
        3. Map syntax could be confused with a map parameter
        4. Default parameters are also given using assignment operator, so why use a different operator when assigning parameters a value during call time ?
        5. Supplies clear separation from existing limited map based default parameters support

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              emge mgroovy
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: