松开“分配给过程变量的局部过程/函数"优雅地限制 [英] Loosen "Local procedure/function assigned to procedure variable" restriction gracefully

查看:10
本文介绍了松开“分配给过程变量的局部过程/函数"优雅地限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下测试用例:

{ CompilerVersion = 21 }程序全局();程序本地();开始结尾;类型TProcedure = 程序();无功过程:T过程;开始过程:=本地;{ E2094 分配给过程变量的本地过程/函数Local"}结尾;

在第 13 行,编译器发出带有 ERROR 级别的消息,禁止此类本地过程使用的所有情况.官方"解决方案是将Local符号提升到外部作用域(即:使其成为Global的兄弟),这将对代码结构化"产生负面影响.><小时>

我正在寻找以最优雅的方式绕过它的方法,最好是让编译器发出警告级别的消息.

解决方案

最好的办法是使用新的匿名方法功能将其声明为 对过程的引用,然后您就可以很好地封装所有内容.

类型TProc = 程序参考;程序外;无功本地:TProc;开始本地 := 程序开始做东西;结尾;当地的;结尾;

这解决了梅森描述的问题,方法是捕获匿名函数的任何局部变量.

Consider the following test-case:

{ CompilerVersion = 21 }
procedure Global();

  procedure Local();
  begin
  end;

type
  TProcedure = procedure ();
var
  Proc: TProcedure;
begin
  Proc := Local;  { E2094 Local procedure/function 'Local' assigned to procedure variable }
end;

At line 13 compiler emits message with ERROR level, prohibiting all of the cases of such local procedures usage. "Official" resolution is to promote Local symbol to the outer scope (ie: make it a sibling of Global) which would have negative impact on code "structuredness".


I'm seeking the way to circumvent it in most graceful manner, preferably causing compiler to emit WARNING level message.

解决方案

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.

这篇关于松开“分配给过程变量的局部过程/函数"优雅地限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆