Bug 5134 – std.algorithm.startsWith won't accept var from "in" as first arg
Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-10-29T23:57:00Z
Last change time
2011-07-12T11:25:38Z
Keywords
rejects-valid
Assigned to
andrei
Creator
bus_dbugzilla
Comments
Comment #0 by bus_dbugzilla — 2010-10-29T23:57:54Z
import std.algorithm;
void main()
{
foo("hello");
}
void foo(in string str)
{
startsWith(str, "a");
}
DMD 2.050 output:
testStartsWith.d(8): Error: template std.algorithm.startsWith(alias pred = "a == b",Range,Ranges...) if (Ranges.length > 1 && isInputRange!(Range) && is(typeof(.startsWith!(pred)(doesThisStart,withOneOfThese[0])) : bool) && is(typeof(.startsWith!(pred)(doesThisStart,withOneOfThese[1..__dollar])) : uint)) does not match any function template declaration
testStartsWith.d(8): Error: template std.algorithm.startsWith(alias pred = "a == b",Range,Ranges...) if (Ranges.length > 1 && isInputRange!(Range) && is(typeof(.startsWith!(pred)(doesThisStart,withOneOfThese[0])) : bool) && is(typeof(.startsWith!(pred)(doesThisStart,withOneOfThese[1..__dollar])) : uint)) cannot deduce template function from argument types !()(const(immutable(char)[]),string)
Worked in DMD 2.049
Comment #1 by yebblies — 2011-07-12T05:17:02Z
This is not a regression in dmd, but the result of fixing a dmd accepts-invalid.
I'll leave it up to the phobos developers to decide if this is a regression or not. There are other similar bugs in bugzilla, and also enhancement requests for dmd that may make this valid code again.
Comment #2 by issues.dlang — 2011-07-12T11:25:38Z
Of course startsWith won't accept something which was an in parameter. That would make it const. And a const range is useless. startsWith could work for const arrays if templates were smart enough to realize that (see bug# 6148), but they're not. They instantiate with the exact type that you give them, and a const array can't have popFront called on it, so it's useless in a range-based function.
Now, if the fix for bug# 6289 gets pulled in, then it'll be possible to use const arrays with range-based functions by slicing them
void foo(in string str)
{
startsWith(str[], "a");
}
but as long as a slice of an array is the exact same type as the original instead of making the slice mutable (leaving the elements at the appropriate level of mutability of coures - immutable in the case of string), that doesn't work. You can cast the string - startsWith(cast(string)str, "a") - and it should work just fine, but as long str is const, it won't. So, this is not a bug that's going to be fixed by changing anything with startsWith. It's an inherent limitation in the language with regards to templates. Improving the situation with slicing const/immutable arrays should help, because then you can just slice them (as you would have to do with a static array), but until then, a cast is your best option.
Of course, I would point out that having a string be in is of debatable value in the first place, since the original array can't be altered anyway. All it does is make it so that str can't be reassigned to another string inside of foo. Granted, you may want that, but in plenty of cases, it really doesn't buy you much.
So, this bug really isn't a bug. Fixing bug# 6289 would make slicing the string work, which would effectively fix this issue, and if bug# 6148 were ever implemented, then that would definitely fix it (though it's questionable whether that's ever going to happen), but as it stands, startsWith isn't doing anything wrong. If it worked before, it was a bug in dmd.