How to resolve "conflicts with" errors in d?

I am trying to compile some D. The code I wrote uses a library std.string

as well std.algorithm

. One of my functions calls indexOf

in line: unfortunately, apparently there is also a function indexOf

in std.algorithm

, and the compiler doesn't like it:

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

      

How do I get around this? In C ++, I could use ::

to explicitly indicate which namespace I am in ... what about D?

+3


source to share


1 answer


If you want to explicitly call std.string.indexOf

, then std.string.indexOf(str, c)

instead of indexOf(str, c)

or str.indexOf(c)

.

Or you can use an alias:

alias std.string.indexOf indexOf;

      

If you put this inside the function you are calling in indexOf

, then it should be treated indexOf

like the std.string.indexOf

rest of the function. Or, if you put it at the module level, it will affect the whole module.



However, due to a bug, UFCS (Universal Function Call Syntax) does not currently work with local aliases, so if you put the alias inside the function, you will need to do indexOf(str, c)

instead str.indexOf(c)

.

The third option is to use selective imports:

import std.string : indexOf;

      

With this import, only std.string is imported indexOf

, and when you use indexOf

it will use the version string

(even if you import std.algorithm as well). And you can even import std.string regularly in addition to selective import to get the rest of the std.string and selective import will still resolve the conflict (in which case it is really no different from importing std.string and then aliases indexOf

) ... However, due to the bug, selective imports are always treated as open, so selective imports indexOf

in a module will affect every module that imports it (potentially causing new conflicts), so you can avoid that for now.

+7


source







All Articles