applay for template function with sumtypes result?
Steven Schveighoffer
schveiguy at gmail.com
Tue Jul 4 12:43:19 UTC 2023
On 7/4/23 12:58 AM, kiriakov wrote:
> Hi. I can't handle template. It all looks sad, build ok, test error.
>
> ```
> struct ParsResult(T) { T[] _in; T[] _out; }
> struct Err(T) { T[] x; }
>
> struct Result(T) {
> SumType!(ParsResult!T, Err!T) data;
> alias data this;
> this(Value)(Value value) { data = value; }
> }
>
>
> Result!T applay(T)(Result!T function (T[]) pure fun, T[] x) pure {
> return fun(x); }
> ParsResult!char f (char[] x) pure { return ParsResult!char(x[1..$],
> x[2..$]); }
>
> unittest
> {
> writeln(applay(&f,"String10".dup));
> }
> ```
>
> I got
>
> Error: none of the overloads of template `mexception.pars` are callable
> using argument types `!()(ParsResult!char delegate(char[] x) pure
> nothrow @nogc @safe, char[])`
> source/mexception.d(64,10): Candidate is: `pars(T)(Result!T
> function(T[]) pure fun, T[] x)`
>
If inference isn't working you can explicitly instantiate the template
to see why it isn't working.
After adding the necessary imports, I changed your unittest to:
```d
writeln(applay!char(&f,"String10".dup));
```
And I get the errors:
```
onlineapp.d(18): Error: function
`onlineapp.applay!char.applay(Result!char function(char[]) pure fun,
char[] x)` is not callable using argument types `(ParsResult!char
function(char[] x) pure, char[])`
onlineapp.d(18): cannot pass argument `& f` of type
`ParsResult!char function(char[] x) pure` to parameter `Result!char
function(char[]) pure fun`
```
It looks like a type mismatch. The function accepted should return
`Result` but it's returning `ParsResult`.
-Steve
More information about the Digitalmars-d-learn
mailing list