splitter string/char different behavior

Jon Degenhardt jond at noreply.com
Sat Sep 30 18:21:11 UTC 2017


On Saturday, 30 September 2017 at 17:17:17 UTC, SrMordred wrote:
> writeln( "a.b.c".splitter('.').dropBack(1) ); //compiles ok
> writeln( "a.b.c".splitter(".").dropBack(1) );
>
> //error:
> Error: template std.range.dropBack cannot deduce function from 
> argument types !()(Result, int), candidates are:
> (...)
>
> Hm.. can someone explain whats going on?

It's easy to overlook, but documentation for splitter starts out:

      Lazily splits a range using an element as a separator.

An element of a string is a char, not a string. It needs to be 
read somewhat literally, but it is correct.

It's also part of template constraint, useful once you've become 
accustomed to reading them:

     auto splitter(alias pred = "a == b", Range, Separator)(Range 
r, Separator s)
         if (is(typeof(binaryFun!pred(r.front, s)) : bool) && ....

For "a.b.c"splitter(x), Range r is a string, r.front is a char. 
The template can only be instantiated if the predicate function 
is valid. The predicate function is "a == b". Since r.front is a 
char, then s must be a type that can be compared with '=='. A 
string and char cannot be compared with '==', which is why the a 
valid template instantiation could not be found.



More information about the Digitalmars-d-learn mailing list