2

I try to revise some LEGACY CODE that I wrote back in the Delphi 10.1 Berlin. Unfortunately, it does not compile in the current Delphi 11 Alexandria, as it shows this compiler error message:

[dcc32 Error] AppDocUtils.pas(1164): Cannot capture symbol 'FileSearch'

procedure PACountFilesInAppDocsTask(const ADir: string; const BAllApps: Boolean);
var
  ThisTask: ITask;
begin
  ThisTask := TTask.Create(
    procedure()
    var
      count: Integer;
    
      procedure FileSearch(const ThisDir: string);
      var
        SR: System.SysUtils.TSearchRec;
      begin
        if System.SysUtils.FindFirst(IncludeTrailingPathDelimiter(ThisDir) + '*', faAnyFile, SR) = 0 then
        begin
          try
            repeat
              if (SR.Attr and faDirectory) = 0 then
              begin
                Inc(Count);
              end
              else if (SR.Name <> '.') and (SR.Name <> '..') then
              begin
                FileSearch(IncludeTrailingPathDelimiter(ThisDir) + SR.Name);
              end;
            until FindNext(SR) <> 0;
          finally
            System.SysUtils.FindClose(SR);
          end;
        end;
      end;
    
    begin
      count := 0;
      FileSearch(ADir);
      TThread.Synchronize(nil,
        procedure
        begin
          if BAllApps then
            FilesCountInAllApps := count
          else
            FilesCountInThisApp := Count;
        end);
    end);
  ThisTask.Start;
end; // error HERE!
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user1580348
  • 5,721
  • 4
  • 43
  • 105
  • Are anonymous procedures even allowed to have local procedures? I've never seen that used before. I would change `FileSearch()` into a standalone procedure that returns the appropriate `count` for its invocation, and then the anonymous procedure can call it and increment its local `Count` with that result. – Remy Lebeau Jul 11 '22 at 18:46
  • I agree with @Remy. I had a quick look in the docs, but couldn't find anything specific about local procedures in anonymous methods. Interesting question, +1. – Andreas Rejbrand Jul 11 '22 at 21:39
  • Probably worth [reporting a compiler bug to Embarcadero](https://quality.embaradero.com). – Remy Lebeau Jul 11 '22 at 22:28
  • Feels like the compiler bug was in previous versions – David Heffernan Jul 11 '22 at 22:32
  • Local PROCEDUREs or FUNCTIONs in anonymous methods generally works fine (use them quite frequently, also in Alexandria). That's not the issue here. There must be another issue with the actual _content_ of the local subroutine that can't be captured... – HeartWare Jul 12 '22 at 07:56

0 Answers0