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

后端 未结 4 1181
臣服心动
臣服心动 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:49

    For the records, my homebrewn closure:

    { this type looks "leaked" }
    type TFunction = function (): Integer;
    
    function MyFunction(): TFunction;
    
      {$J+ move it outside the stack segment!}
      const Answer: Integer = 42;
    
      function Local(): Integer;
      begin
        Result := Answer;
        { just some side effect }
        Answer := Answer + Answer div 2;
      end;
    
    begin
      Result := @Local;
    end;
    
    
    procedure TForm1.FormClick(Sender: TObject);
    var
      Func: TFunction;
      N: Integer;
    begin
      { unfolded for clarity }
      Func := MyFunction();
      N := Func();
      ShowMessageFmt('Answer: %d', [N]);
    end;
    

提交回复
热议问题