是“隐含地导入”总是一个坏的东西在Delphi包中? [英] Is "implicitly imported" always a bad thing in Delphi packages?

查看:126
本文介绍了是“隐含地导入”总是一个坏的东西在Delphi包中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试重新排列一组 TFrame-descendent组件的包,我找到它似乎有必要打破一些我的实用程序TFrame后裔与使用它们的对话框形式分开,主要是因为前者被作为真实组件注册到调色板,并且似乎有时会混淆IDE相对于使用它们的对话框形式。对话窗体依次由非可视化组件调用,它们是第三个包的一部分。这个到目前为止似乎使大多数编译器的依赖性相关的投诉 /混乱消失。 (我还没出来,但是)。

Trying to rearrange my packages for a set of TFrame-descendent components, I'm finding it seemingly necessary to break out some of my utility TFrame descendents separate from the dialog forms which use them, mainly because the former are registered to the palette as true components and that seems to confuse the IDE sometimes with respect to the dialog forms which use them. The dialog forms in turn are called by non-visual components, which are part of a third package. This, so far, seems to make most of the compiler's dependency-related complaints / confusions go away. (I'm not out yet, however).

当使用对话框窗体(调用框架)编译包时,我获取警告单位MyFrames隐式导入到包MyDialogForms

When compiling the package with the dialog forms (which call the Frames), I am getting the warning "Unit 'MyFrames' implicitly imported into package 'MyDialogForms'"

鉴于它显示为编译器警告,很久以前,人们就觉得隐含进口一个单位一般不是件好事。有没有特定的情况呢?即,隐含输入单位是否可行,和/或适当的做法?如果是,具体情况是什么?

Given that it shows up as a compiler warning, I've long ago gotten the impression that "implicitly importing" a unit is generally not a good thing. Are there specific instances where that is not the case? i.e. where implicitly importing a unit is OK, and/or an appropriate practice?... and if so, what are those specific cases?

推荐答案

以下是问题:

您的程序中只能有一个单元的副本。如果您尝试通过程序包加载相同的单元两次,它将引发异常,并且程序包将不会再次加载。避免这种情况的方法是构造您的软件包,以便在多个软件包中不使用任何单元。

You can only have one copy of a unit in your program. If you try to load the same unit twice via packages, it will raise an exception and the package won't load the second time. The way to avoid this is to structure your packages so that no unit is used in more than one of them.

您编译的每个单元的代码必须在包。编译器将从您在包含部分中声明的所有单元开始,但是这些单元使用的任何其他单元也必须进行编译,以便它可以访问,除非这些单元包含在另一个包中需要列出。这些演员是隐含进口单位。麻烦的是,它们被隐含地导入 ,在包含部分中没有明确说明,他们可以在项目经理中轻松地显示在右侧。这意味着您可能不会注意到您的设备在包中,最后将其放在另一个包中。然后,当您尝试运行程序并加载程序包时,会出现故障。这就是为什么编译器会警告你的。

The code to every unit you compile has to be in the package. The compiler will start with all the units you declare in the contains section, but any other units used by those units also has to be compiled in so it will be reachable, unless those units are contained in another package which is listed under requires. These extras are the "implicitly imported" units. Trouble is, they're imported implicitly, not explicitly stated in the contains section where they'll conveniently show up in the Project Manager off to the right. This means that you might not notice that your unit is in a package, and end up putting it in another one. Then when you try to run your program and load the packages, things break. That's why the compiler warns you about it.

出于某种原因,这是一个警告,而不是错误。只要您了解系统的工作原理,使用隐式导入技术就安全。只要记住,无论您是否声明,这些单位都在包装中。但是再一次,由于他们结束了,无论你是否声明,只是正式添加它们,并节省自己的麻烦可能更简单。

It's a warning, and not an error, for a reason. As long as you understand how the system works, it's technically safe to use implicit imports. Just remember that those units are ending up in the package whether you declare them or not. But then again, since they're ending up there whether you declare them or not, it's probably simpler just to officially add them and save yourself the hassle.

这篇关于是“隐含地导入”总是一个坏的东西在Delphi包中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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