Thread overview
[Issue 5134] New: std.algorithm.startsWith won't accept var from "in" as first arg
Oct 30, 2010
Nick Sabalausky
Jul 12, 2011
yebblies
Jul 12, 2011
Jonathan M Davis
October 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5134

           Summary: std.algorithm.startsWith won't accept var from "in" as
                    first arg
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: cbkbbejeap@mailinator.com


--- Comment #0 from Nick Sabalausky <cbkbbejeap@mailinator.com> 2010-10-29 23:57:54 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5134


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 12, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5134


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies@gmail.com
          Component|DMD                         |Phobos


--- Comment #1 from yebblies <yebblies@gmail.com> 2011-07-12 22:17:02 EST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 12, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5134


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
                 CC|                            |jmdavisProg@gmx.com
         Resolution|                            |INVALID


--- Comment #2 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-07-12 11:25:38 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------