Loosen “Local procedure/function assigned to procedure variable” restriction gracefully

后端 未结 4 1171
臣服心动
臣服心动 2020-12-31 19:30

Consider the following test-case:

{ CompilerVersion = 21 }
procedure Global();

  procedure Local();
  begin
  end;

type
  TProcedure = procedure ();
var
           


        
4条回答
  •  难免孤独
    2020-12-31 19:54

    Your best bet is to declare it as reference to procedure using the new anonymous methods feature and then you can keep everything nicely encapsulated.

    type
      TProc = reference to procedure;
    
    procedure Outer;
    var
      Local: TProc;
    begin
      Local := procedure
        begin
          DoStuff;
        end;
      Local;
    end;
    

    This gets around the issues that Mason describes by capturing any variables local to the anonymous function.

提交回复
热议问题