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

Add real named parameters

    XMLWordPrintableJSON

Details

    • Wish
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • Compiler
    • None

    Description

      Maybe it's too late for 4.x, but having real named parameters instead of/in addition to the current, map-based, named parameters would be really helpful.

      A full featured named parameter implementation would

      • enable type checking
      • find errors at compile time
      • eleminate the need to take care of parameter ordering in method calls
      • enhance code readability
      • be far less error prone

      For example:

      def foo(String param1, String param2 = 'val2', String param3) {} 

      could then be invoked like

      foo(param3: 'val3', param1: 'val1')
      foo(param1: 'val1', param3: 'val3')
      foo(param3: 'val3', param1: 'val1', param2: 'somehing else')

      while

      foo(param2: 'val3', param1: 'val1')

      would lead to a compilation error because 'param3' is missing.

      With map-based named parameters, this is all left to the method author and leads to massive amount of boiler plate code to do the type checking and such in the methods, or if not done, like in the documentation examples, to unexpected runtime errors, for example:

      def foo(Map args) {
        println(args.name)
      }

      could easily be called like

      foo(name: 42)

      without leading to an error although "name" is meant to be String. One has to do something like

      def foo(Map args) {
        if (!(args.name instanceof String)) {
          throw new IllegalArgumentException('name must be of type String.')
        }
        println(args.name)
      }

      to catch this.

      Attachments

        Activity

          People

            Unassigned Unassigned
            dheinric Dirk Heinrichs
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: