D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6547 - Call to std.algorithm.remove causes compile error
Summary: Call to std.algorithm.remove causes compile error
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-23 20:52 UTC by Tim Keating
Modified: 2012-06-08 02:05 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tim Keating 2011-08-23 20:52:31 UTC
This minimal use case generates an error in std.algorithm:

    char[] s = "<html>Some fake HTML for your testing pleasure</html>".dup;
    auto start = s.find("<");
    auto len = s[start..$].find(">");
    s = s.remove(tuple(start, start+len+1));
    writeln(s);

The compile error I get from this is:

/usr/local/Cellar/dmd/2.054/src/phobos/std/algorithm.d(5826): Error: front(src) is not an lvalue
/usr/local/Cellar/dmd/2.054/src/phobos/std/algorithm.d(5826): Error: front(tgt) is not an lvalue

I pulled the most current version of Phobos from github and it has the same problem, though there it occurs on line 5933.
Comment 1 Yuri Gorobets 2012-02-16 03:25:32 UTC
(In reply to comment #0)

This behavior appears to be as designed. The compilation problem is caused by special treatment of the "narrow strings" by std.array:

http://dlang.org/glossary.html#narrow%20strings

import std.array;
import std.algorithm;

void main()
{
    char[] s = "narrow".dup;

    s.remove(0); // Error: template std.algorithm.move(T) 
                 // does not match any function template declaration
                 // Error: template std.algorithm.move(T) cannot deduce 
                 // template function from argument types !()(dchar,dchar)

    // remove troubles are caused by the special treatment of narrow strings
    // front(s) doesn't return a char reference but dchar value instead:
    s.front = 'c';	// Error: s.front is not an lvalue
}

dchar version works fine:

void main()
{
    dchar[] u = "unicode"d.dup;
    u.remove(0);	// works fine
    u.front = 'c';
}
Comment 2 Tim Keating 2012-03-13 15:39:44 UTC
I will say that I don't agree with this assessment -- the bug isn't really about user error while operating on a narrow string, but about how the compiler & library combo respond to that misuse. To wit, anytime a user gets a cryptic error message buried deep inside a library in response to a simple mistake, you have disempowered them from solving their own problem and derailed their momentum in learning the language.

That said, under 2.058 the error message is now:

    test.d(11): Error: cannot implicitly convert expression (start) of type char[] to ulong

Which makes it clear that find(string, string) is returning a slice. I think this specific issue should be closed.
Comment 3 Tim Keating 2012-03-16 12:26:44 UTC
The minimal use case described by Yuri above still occurs with 2.058.

If this isn't supposed to work for narrow strings, then perhaps a template specialization for the unsupported types that raises a useful error message is the right solution here?
Comment 4 SomeDude 2012-04-22 03:24:38 UTC
Reduced to "normal"
Comment 5 Jonathan M Davis 2012-06-08 02:05:32 UTC
As of 2.059, this is the error that you get:

 q.d(10): Error: cannot implicitly convert expression (start) of type char[] to ulong
q.d(11): Error: template std.typecons.tuple does not match any function template declaration
/home/jmdavis/dmd2/linux/bin/../../src/phobos/std/typecons.d(687): Error: template std.typecons.tuple(T...) cannot deduce template function from argument types !()(char[],_error_)

which is much better than the one originally reported.