Bug 4883 – std.algorithm functions conflict with std.string fucntions

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-09-17T15:00:00Z
Last change time
2011-01-21T21:52:52Z
Assigned to
andrei
Creator
seth.a.hoenig

Comments

Comment #0 by seth.a.hoenig — 2010-09-17T15:00:00Z
Given these two minimal programs: import std.string; void main() { string str = "abc"; int i = str.count("ab"); } and: import std.string; import std.algorithm; void main() { string str = "abc"; int i = str.count("ab"); } The only difference is line 2, where I import std.algorithm. The first program compiles fine, but the second program does not compile, spitting out the error message: bash-3.2$ dmd -ofdummy dummy.d /u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(176): Error: static assert "Bad binary function q{a == b}. You need to use a valid D expression using symbols a of type dchar and b of type string." /u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(179): instantiated from here: Body!(dchar,string) /u/sah2659/dmd2/linux/bin/../../src/phobos/std/algorithm.d(3410): instantiated from here: result!(dchar,string) dummy.d(7): instantiated from here: count!("a == b",string,string) A little more investigating reveals that std.algorithm.indexOf and std.string.indexOf also conflict.
Comment #1 by bearophile_hugs — 2010-09-19T17:52:14Z
As Phobos grows, it's harder and harder to to invent all distinct nice names for all its modules, and keep those name not too much long. In this case it may be better to change function names, but in general you need to learn to use qualified imports, and even renamed imports when necessary.
Comment #2 by d-bugzilla — 2010-11-09T09:20:47Z
(Digital Mars D Compiler v2.050) I get the same error message without even importing std.string: $ cat count.d import std.algorithm; void main() { string str = "abc"; int i = str.count("ab"); } $ dmd count.d /usr/include/d/dmd/phobos/std/functional.d(176): Error: static assert "Bad binary function q{a == b}. You need to use a valid D expression using symbols a of type dchar and b of type string." /usr/include/d/dmd/phobos/std/functional.d(179): instantiated from here: Body!(dchar,string) /usr/include/d/dmd/phobos/std/algorithm.d(3300): instantiated from here: result!(dchar,string) count.d(5): instantiated from here: count!("a == b",string,string) Even though I don't expect the program to compile successfully, I find it odd that the compiler picks up std.algorithm.count at all, when I'm using the object.member notation.
Comment #3 by issues.dlang — 2010-11-09T10:21:08Z
The object.member notation is generally irrelevant unless it's actually a member function. str.count("ab") will be changed to count(str, "ab") and any overload resolution will be taken care of that way. The fact that count() is a template function in std.algorithm and not in std.string probably doesn't help any either, given how you can't currently overload functions where one is a template and one not (though they'd be in different overload sets in this case). Regardless, using alias std.string.count count; should fix the problem, allowing you to use the string version without having to write std.string.count(str, "ab"). Still, it would be better if std.string and std.algorithm would avoid name clashes, since they're so likely to conflict. At least some of the name clashes should be going away though as functions are consolidated.
Comment #4 by andrei — 2011-01-21T21:52:52Z
Fixed by diffs http://www.dsource.org/projects/phobos/changeset/N where N is 2339, 2340, 2341, 2342, 2343, 2349, 2350, 2354