www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Name collision using std.regex and std.array / std.range

reply Tobias Pankrath <tobias pankrath.net> writes:
I needed a small script for a project of mine and hacked one in python. 
Since I want to learn some D, I tried to convert it. Took me a bit longer 
than I thought, because I got delayed by this lovely error message:

Error: template instance ambiguous template declaration 
std.array.replace(R1,R2,R3) if (isDynamicArray!(R1) && isForwardRange!(R2) 
&& isForwardRange!(R3) && (hasLength!(R3) || isSomeString!(R3))) and 
std.regex.replace(Range,Engine,String) if (is(Unqual!(Engine) == Regex!
(Unqual!(typeof(Range.init[0])))))

Took me a while do understand it, but it seems that I've got a name 
collision. What I don't understand is, why is the compiler unable to 
tell two functions, one with two parameter and one with three, apart?
Jul 24 2011
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 24.07.2011 20:46, Tobias Pankrath wrote:
 I needed a small script for a project of mine and hacked one in python.
 Since I want to learn some D, I tried to convert it. Took me a bit longer
 than I thought, because I got delayed by this lovely error message:

 Error: template instance ambiguous template declaration
 std.array.replace(R1,R2,R3) if (isDynamicArray!(R1)&&  isForwardRange!(R2)
 &&  isForwardRange!(R3)&&  (hasLength!(R3) || isSomeString!(R3))) and
 std.regex.replace(Range,Engine,String) if (is(Unqual!(Engine) == Regex!
 (Unqual!(typeof(Range.init[0])))))

 Took me a while do understand it, but it seems that I've got a name
 collision. What I don't understand is, why is the compiler unable to
 tell two functions, one with two parameter and one with three, apart?
Apparently, this overload of replace has 3 parameters: input, what to replace and a replacement. It searches and replaces all occurrences. I've hit the same issue while implementing a replacement for std.regex, the underlying problem I think is that std.array.replace has a way too lousy template constraint. According to source it checks only if it's input is a range or string, but if it has compatible type is *not* checked. Now with std.regex since RegexMatch is ForwardRange it's of course matches, but element type is string/wstring/dstring, while I suppose std.array in this case would need element type of dchar. It hadn't occurred to me that somebody will hit this anytime soon, so now I might roll out a pull request sooner :) -- Dmitry Olshansky
Jul 24 2011