我正在寻找类似于private属性的东西,而不是与类成员一起工作,而是使用模块实体。 函数,类,枚举等
例如,在Erlang中有一个导出属性,如下所示: -export([fun1, fun2]) ,这意味着模块中所有功能的fun1和fun2都将被导出。 在D中,一切似乎都默认导出,这没问题,但有没有办法阻止某些具体的东西?
I'm looking for something like private attribute working not with class members, but with module entities. Functions, classes, enumerators etc.
For instance, in Erlang there is an export attribute, which goes like this: -export([fun1, fun2]), meaning only fun1 and fun2 of all the functions in the module would be exported. In D everything seems to be exported by default, which is ok, but is there a way to prevent something specific from that?
最满意答案
在被导入的模块中,你可以标记任何私有的东西,以防止它被其他模块访问。 private在模块级实体上的工作方式与在类成员上的工作方式相同 - 在模块外部不可访问,在模块内部可用。 但是,目前它仍然是可见的 ,所以它可以创建像“来自模块A的私有函数foo与来自模块B的函数foo冲突”的愚蠢错误,从而迫使您消除该名称的歧义。 (我和其他几个人都希望在某个时候改变它,因为它显然不应该是一个问题!)
在进行导入的模块中,您不能说“导入所有除外”,但您可以导入特定名称的列表而不导入其他名称:
import std.stdio : File, writefln; void main() { File f; // cool writefln("hello"); // cool writeln("hey"); // "Error: 'writeln' is not defined" - the selective import didn't pull this name at all }In the module being imported, you can mark anything private to keep it from being accessible from other modules. private works the same way on module level entities as it does on class members - inaccessible outside the module, usable inside the module. However, currently it is still visible, so it can create silly errors like "private function foo from module A conflicts with function foo from module B", forcing you to disambiguate the name. (I, and several others, are hoping to get this changed at some point, since it obviously shouldn't be a problem!)
In the module doing the importing, you can't say "import all except", but you can import a list of specific names without importing others:
import std.stdio : File, writefln; void main() { File f; // cool writefln("hello"); // cool writeln("hey"); // "Error: 'writeln' is not defined" - the selective import didn't pull this name at all }更多推荐
发布评论