pointer to std.algorithm.iteration : splitter

Dukc ajieskola at gmail.com
Thu Aug 31 10:46:53 UTC 2023


On Thursday, 31 August 2023 at 05:16:02 UTC, Vino wrote:
> Hi All,
>
>   Request your help on the below error
>
> Program
> ```
> void main()
> {
> 	import std.stdio:writeln;
> 	import std.algorithm.iteration : splitter;
> 	
> 	auto splitter_ptr = &splitter!((a, b) => a.splitter(b).array);
>     string str = "TEST1;TEST2;TEST3";
>     auto words = splitter_ptr(str, ';');
>     foreach (word; words) { writeln(word); }
> }
> ```
> ```
> Error: template instance `splitter!((a, b) => 
> a.splitter(b).array)` does not match any template declaration
> ```
>
> From,
> Vino

I'm assuming you want to instantiate `splitter` and take the 
address of the resulting function, so that `splitter_ptr` will be 
a function pointer or a delegate.

You are getting the compile time arguments to `splitter` utterly 
wrong. It should take:

  - A function that checks equality between an element and a 
separator.

  - a `keepSeparators` flag.

  - type of the range you're passing in, `string` in this case.

  - type of the separator, `char` or `dchar` in this case.

You can find this all in [the 
docs](https://dlang.org/phobos/std_algorithm_iteration.html#.splitter). I think you accidentally passed what you wanted to do with `splitter` as a compile-time parameter to it.

I don't think it's very practical to try instantiating a complex 
templated function like `splitter` without calling it. It's just 
not designed for that. I'd rather define a function that wraps a 
call to it, like (not tested, may have errors):

```D
auto splitter_ptr = (string str, dchar sep) => 
str.splitter(sep).array;
```

Maybe that's what you tried in the first place. You don't need 
`&` here, because a lambda (which we use here) is already a 
function pointer (or a delegate).


More information about the Digitalmars-d-learn mailing list