好像有没有简单的方法来防止D的import语句从混浊的全局命名空间:只要你import std.stdio
我可以避免使用D导入来污染公共名称空间吗?
module x;
import std.stdio;
void main() {
writeln("Hello!");
}
,writeln
现在是全球性的。来自[language with namespaces]
,这将是很好,如果我只能参考std.stdio.writeln
,特别是在一两周内,我可以很容易地告诉什么提供writeln
。
阅读Namespaces with classes and structs?后,在我看来,这应该做想我(作为笨拙,因为它是):
module x;
import std.stdio;
class String {
struct write {
auto println = writeln;
}
}
void main() {
String string = new String();
string.write.println("Hello!");
}
,但我得到Error: variable [...] type void is inferred from initialiser [...], and variables cannot be of type void
,这意味着功能走样了。如果我写C,我可以理解命名空间的缺乏(但我仍然可以用结构和点符号来实现它们)。有没有办法让导入的名称不是如此全球?
只记得'静态导入'是一件事情。这可能是你正在寻找的东西。 – rcorre
你的意思是 - 你刚刚阅读我的评论。 ;) – DejanLekic
哎呀,没有意识到你在我编辑的时候回答了。你的答案可能更好,因为它削减了追逐的权利:) – rcorre