2013-02-15 68 views
3

我想编译一些D.我编写的代码使用std.string库以及std.algorithm。我的一个功能上的绳子呼吁indexOf:不幸的是,显然也有在std.algorithm一个indexOf函数,编译器不喜欢它:如何解决与d中的“冲突”错误?

assembler.d(81): Error: std.algorithm.indexOf!("a == b", string, immutable(char)).indexOf at /usr/share/dmd/src/phobos/std/algorithm.d(4431) conflicts with std.string.indexOf!(char).indexOf at /usr/share/dmd/src/phobos/std/string.d(334) 
assembler.d(81): Deprecation: function std.algorithm.indexOf!("a == b", string, immutable(char)).indexOf is deprecated 

如何解决此得到什么?在C++中,我可以使用::明确地说出我在哪个命名空间...... D呢?

回答

7

如果您想明确地拨打std.string.indexOf,请执行std.string.indexOf(str, c)而不是indexOf(str, c)str.indexOf(c)

或者你可以使用一个别名:

alias std.string.indexOf indexOf; 

如果你把在那里你调用indexOf里面的功能,那么就应该再考虑indexOfstd.string.indexOf为函数的其余部分。或者如果你把它放在模块级别,那么它会影响整个模块。

然而,由于,UFCS(通用函数调用语法)目前不与本地别名工作,所以如果你把别名功能中,你就必须做indexOf(str, c)而不是str.indexOf(c)

第三种选择是使用选择性导入:

import std.string : indexOf; 

随着该进口,只有indexOf从std.string进口的,当你使用indexOf,它会使用string版本(即使你也导入了std.algorithm)。除了选择性导入之外,您甚至可以定期导入std.string以获取std.string的其余部分,而选择性导入仍然可以解决冲突(在这种情况下,它与导入std.string没有什么不同,然后别名indexOf)。但是,由于bug,选择性导入始终被视为公共,因此在模块中选择性导入indexOf会影响导入它的每个模块(可能导致新的冲突),因此您可能希望在此时避免它。