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

asymmetry of closure parameter type among String#replaceAll/String#eachMatch/Pattern#each

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.7.6, 1.8-beta-3
    • groovy-jdk
    • None
    • MacOS X

    Description

      The closure which passed to String#replaceAll() takes arguments in two patterns:

      1) Object[]
      2) Arguments correspond to matcher's group(0), group(1), group(2) ...

      but the closure for Pattern#match(Closure) and String#eachMatch(Closure) takes:

      1) List
      2) Arguments correspond to matcher's group(0), group(1), group(2) ...

      So there is an asymmetry about 1).

      If this issue should be fixed, IMHO, to change replaceAll() to take a list like
      match/eachMatch is better. Because you can write simply:

      assert "ABDE" == "abcdef".replaceAll(/(..)(.)/) { it[1].toUpperCase() } // (A)
      

      rather than:

      assert "ABDE" == "abcdef".replaceAll(/(..)(.)/) { Object[] it -> it[1].toUpperCase() } //(B)
      

      (A) is same as:

      assert "ABDE" == "abcdef".replaceAll(/(..)(.)/) { List it -> it[1].toUpperCase() } // (C)
      

      I attach small patch for 1.8-trunk and some test cases for this.
      This patch leads small backward-incompatibility so that (B) would fail because
      of the difference of parameter type of the closure. But current test cases
      don't include this.

      Attachments

        1. patch3
          4 kB
          UEHARA Junji
        2. patch2
          2 kB
          UEHARA Junji
        3. patch
          2 kB
          UEHARA Junji

        Activity

          People

            guillaume Guillaume Sauthier
            uehaj UEHARA Junji
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: