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

Multiple assignment

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.6-rc-2
    • GEP
    • None

    Description

      Support for multiple assignment to simulate multiple return types:

      a, b, c = someFunctionReturningAListOfThreeElements()

      If more elements are returned than the number of variables to assign values to, we could get the head of list in the first variable and the rest of the list in the second variable:

      head, tail = [1,2,3,4]
      assert head == 1
      assert tail == [2,3,4]

      Can also be used for variable swapping
      x, y = [y, x]
      x, y = y, x

      We have to be careful with varibale definitions, because currently:
      def a, b, c = someFunctionReturningList()
      because currently, a and b would be null, while c would be assigned to the value of the return of the function.

      A GEP should be created to present all the possibilities, syntax, edge cases.

      Further ideas (subsequent feature enhancements) could be considered like fetching matching groups from regex:
      def regex = ~/firstname: (.), name: (.)/
      firstname, name = ("firstname: Guillaume, name: Laforge") =~ regex)
      assert firstname == "Guillaume"
      assert name == "Laforge"

      Attachments

        Issue Links

          Activity

            People

              blackdrag Jochen Theodorou
              jstrachan James Strachan
              Votes:
              2 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: