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

recursion does not work with clousures

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Won't Fix
    • 1.5.6
    • 1.5.7
    • None
    • None

    Description

      The following code can not be executed and a exeception was throwed when I tried to execute it:
      groovy.lang.MissingMethodException: No signature of method: Script3.listPaths() is applicable for argument types: (java.io.File) values:

      {d:\temp\dos2unix.bat.txt}
      def listPaths = { File location ->
          def result = []
      
          result << location.canonicalPath
      
          if (location.isDirectory()) {
              File[] files = location.listFiles()
              
              for (f in files) {
                  result.addAll(listPaths(f))
              }
          }
      
          return result
      }
      
      listPaths(new File('d:/temp'))
      

      Luckily, we can use the following method which is same with the above closure in terms of function.

      Could you tell me why the implementation with closure failed? Thanks!

      def listPaths(File location) {
          def result = []
      
          result << location.canonicalPath
      
          if (location.isDirectory()) {
              File[] files = location.listFiles()
              
              for (f in files) {
                  result.addAll(listPaths(f))
              }
          }
      
          return result
      }
      
      listPaths(new File('d:/temp'))
      

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            daniel_sun Daniel Sun
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: