是“隐式导入"的Delphi 软件包中总是一件坏事? [英] Is "implicitly imported" always a bad thing in Delphi packages?

查看:25
本文介绍了是“隐式导入"的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).

使用对话框表单编译包时(调用框架),我收到警告Unit '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.

您编译的每个单元的代码都必须在包中.编译器将从您在 contains 部分中声明的所有单元开始,但这些单元使用的任何其他单元也必须在其中编译,以便可以访问,除非这些单元包含在另一个包中列在需要下.这些额外的东西是隐式导入"的单位.问题是,它们是隐式导入的,没有在包含部分明确说明,它们会方便地显示在右侧的项目管理器中.这意味着您可能不会注意到您的设备在一个包装中,而最终将其放入另一个包装中.然后,当您尝试运行程序并加载程序包时,事情就会中断.这就是编译器警告你的原因.

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天全站免登陆