是否可以添加“断言”作为 Delphi 中的关键字?

发布于 2024-08-27 08:19:23 字数 161 浏览 6 评论 0原文

我在代码中编写了几个“assert(...)”,以确保满足前置条件和后置条件。我们可以告诉Delphi编译器,是否在调试版本中使用断言进行编译,而在发布版本中是否使用断言进行编译。

我想知道,是否可以像其他 Pascal 关键字一样突出显示“assert”?

I write couple of "assert(...)" in code, to make sure that pre- and post-conditions should be satisfied. We can tell the Delphi compiler, whether to compile with assertions in a debug version and without assertions in a release version.

I would like to know, if it is possible, to highlight "assert" like other Pascal keywords?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无戏配角 2024-09-03 08:19:23

Assert 与许多其他看似“保留字”一样,实际上是所谓的“标准函数”。

Exit、Break、Continue 和 Assert 是编译器不保留的符号的示例,而是作为符号存在于“系统”单元中命名空间。原因是 Delphi(或更具体地说 Turbo Pascal)最初没有 Exit、Break、Continue 或 Assert 的概念。许多现有用户可能已经在他们自己的库中使用了这些标识符。如果我们将这些标识符设为保留字,我们就会破坏许多现有的应用程序。通过将它们创建为“标准函数”并将其“范围”限定到系统单元,我们可以提供附加功能,而无需担心破坏现有代码的风险。在某些具有自己的 Exit 函数的库中,程序对该标识符的使用不会受到影响。但是,如果程序员明确想要使用 Exit 标准函数,则可以完全限定标识符,如“System.Exit”,编译器将生成退出当前函数的代码,而不是调用范围更接近的 Exit。

Assert, like many other seemingly "reserved words" are actually what are called "standard functions"

Exit, Break, Continue, and Assert are examples of symbols that are not reserved by the compiler, but rather exist as symbols within the "System" unit namespace. The reason is that Delphi (or more specifically Turbo Pascal) originally didn't have the notion of Exit, Break, Continue or Assert. Many existing users may have already used those identifiers for their own libraries. Had we made these identifiers reserved words, we'd have broken many existing applications. By creating them as "standard functions" and "scoping" them to the System unit, we could provide the added functionality without the risk of breaking existing code. In some library that has, say, its own Exit function, the program's use of that identifier would not be affected. However if the programmer explicitly wanted to use the Exit standard function, you can fully qualify the identifier like this "System.Exit" and the compiler will generate code to exit the current function rather than calling the Exit that is closer in scope.

我不在是我 2024-09-03 08:19:23

实际上Assert 不是保留字
您可以创建自己的 Assert 过程或函数,即使不推荐,它也会编译。
因此,Assert 不可能突出显示。

Actually Assert is not a reserved word.
You can create your own Assert procedure or function and even though it's not recommended, it will compile.
Therefore no highlighting is possible with Assert.

嘿嘿嘿 2024-09-03 08:19:23

不可以。Delphi 的荧光笔不能这样配置。您可以配置关键字的颜色,但无法配置哪些字词算作关键字。

如果您将此问题视为功能请求,那么请将其提交至 Embarcadero 的 质量中心。 Stack Overflow 不是这样的场所。

由于您正在检查前置条件和后置条件,因此即使不突出显示,您的断言也应该很容易找到:它们将是任何函数中的第一个和最后一个语句。

No. Delphi's highlighter is not configurable like that. You can configure the color of keywords, but you cannot configure what words count as keywords.

If you meant this question as a feature request, then please submit it to Embarcadero's Quality Central. Stack Overflow is not the venue for that.

Since you're checking preconditions and postconditions, your assertions should be pretty easy to find even without highlighting: They'll be the first and last statements in any function.

风筝在阴天搁浅。 2024-09-03 08:19:23

我总是用大写字母输入退出函数以使其脱颖而出。你可以尝试用断言做同样的事情。

I always type the exit function in CAPITALS to make it stand out. you could try the same with assert.

无语# 2024-09-03 08:19:23

我不这么认为。在工具 -> 下选项->编辑器选项 ->源选项,您可以打开或关闭语法突出显示,但我看不到任何选项可以让您定义要突出显示的语法。

I don't think so. Under Tools -> Options -> Editor Options -> Source Options, you can turn syntax highlighting on or off, but there's no option I can see that lets you define what syntax to highlight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文