gpt4 book ai didi

namespaces - 如何解决 d 中的 "conflicts with"错误?

转载 作者:行者123 更新时间:2023-12-03 12:10:01 26 4
gpt4 key购买 nike

我正在尝试编译一些 D。我编写的代码使用了 std.string 库以及 std.algorithm。我的一个函数在字符串上调用 indexOf:不幸的是,显然 std.algorithm 中还有一个 indexOf 函数,而编译器没有喜欢它:

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 呢?

最佳答案

如果你想显式调用 std.string.indexOf,那么执行 std.string.indexOf(str, c) 而不是 indexOf(str, c)str.indexOf(c)

或者您可以使用别名:

alias std.string.indexOf indexOf;

如果你把它放在调用 indexOf 的函数中,那么它应该将 indexOf 视为 std.string.indexOf 用于其余功能。或者如果你把它放在模块级别,那么它会影响整个模块。

但是,由于 bug , 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 , 选择性导入总是被视为公共(public)的,所以在模块中选择性导入 indexOf 会影响导入它的每个模块(可能导致新的冲突),所以此时你可能想避免它.

关于namespaces - 如何解决 d 中的 "conflicts with"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14887954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com