dphobos

Why Can't I Use std.algorithm.count With a Predicate Function


The following code fails to compile:

assert("(((())))()()()()))".count!(c => c.among!('(', ')')) > 0);

With the error message:

"Error: template std.algorithm.searching.count cannot deduce function from argument types !((c) => c.among!('(', ')'))(string), candidates are..."

But the [standard library (http://dlang.org/phobos/std_algorithm_searching.html#.count) clearly shows that there is an overload of count that takes a predicate, counting all element of R for which the predicate returns true. So why does the compiler complain when I try to use count this way?


Solution

  • assert("(((())))()()()()))".count!(c => c.among!('(', ')') != 0) > 0);

    The problems are:

    1. That your lambda is returning uint instead of bool (check the documentation for the return value of among).
    2. That the compiler error is not helpful.