2

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?

Nordlöw
  • 11,838
  • 10
  • 52
  • 99
Meta
  • 1,091
  • 6
  • 14

1 Answers1

7

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.
  • 3
    That's odd, the template constraint specifically says `is(typeof(unaryFun!pred(haystack.front)) : bool)`. I would've thought that `is(uint: bool)` would return true, but I guess not. – Meta Oct 09 '15 at 21:05