www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - applay for template function with sumtypes result?

reply kiriakov <itcraft.letter gmail.com> writes:
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)`
Jul 03 2023
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Jul 04 2023
parent reply kiriakov <itcraft.letter gmail.com> writes:
On Tuesday, 4 July 2023 at 12:43:19 UTC, Steven Schveighoffer 
wrote:
 On 7/4/23 12:58 AM, kiriakov wrote:
 It looks like a type mismatch. The function accepted should 
 return `Result` but it's returning `ParsResult`.

 -Steve
That's the problem Result(T) = SumType!(ParsResult!T, Err!T)
Jul 04 2023
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/4/23 10:19 AM, kiriakov wrote:
 On Tuesday, 4 July 2023 at 12:43:19 UTC, Steven Schveighoffer wrote:
 On 7/4/23 12:58 AM, kiriakov wrote:
 It looks like a type mismatch. The function accepted should return 
 `Result` but it's returning `ParsResult`.
That's the problem Result(T) = SumType!(ParsResult!T, Err!T)
No, `Result(T)` is its own struct in your example. And also, `SumType!(ParsResult!T, Err!T)` is not the same as `ParsResult!T`. function pointers must match the return type. -Steve
Jul 04 2023
parent reply kiriakov <itcraft.letter gmail.com> writes:
On Tuesday, 4 July 2023 at 14:42:31 UTC, Steven Schveighoffer 
wrote:
 On 7/4/23 10:19 AM, kiriakov wrote:
 On Tuesday, 4 July 2023 at 12:43:19 UTC, Steven Schveighoffer 
 wrote:
 On 7/4/23 12:58 AM, kiriakov wrote:
 It looks like a type mismatch. The function accepted should 
 return `Result` but it's returning `ParsResult`.
That's the problem Result(T) = SumType!(ParsResult!T, Err!T)
No, `Result(T)` is its own struct in your example. And also, `SumType!(ParsResult!T, Err!T)` is not the same as `ParsResult!T`. function pointers must match the return type. -Steve
I was recommended: https://forum.dlang.org/thread/iblsgqqafagdgcsafhxv forum.dlang.org ``` struct Result(T) { SumType!(ParsResult!T, Err!T) data; alias data this; this(Value)(Value value) { data = value; } } ```
Jul 04 2023
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/4/23 11:15 AM, kiriakov wrote:
 On Tuesday, 4 July 2023 at 14:42:31 UTC, Steven Schveighoffer wrote:
 On 7/4/23 10:19 AM, kiriakov wrote:
 On Tuesday, 4 July 2023 at 12:43:19 UTC, Steven Schveighoffer wrote:
 On 7/4/23 12:58 AM, kiriakov wrote:
 It looks like a type mismatch. The function accepted should return 
 `Result` but it's returning `ParsResult`.
That's the problem Result(T) = SumType!(ParsResult!T, Err!T)
No, `Result(T)` is its own struct in your example. And also, `SumType!(ParsResult!T, Err!T)` is not the same as `ParsResult!T`. function pointers must match the return type.
I was recommended: https://forum.dlang.org/thread/iblsgqqafagdgcsafhxv forum.dlang.org ``` struct Result(T) {      SumType!(ParsResult!T, Err!T) data;      alias data this;      this(Value)(Value value) { data = value; } } ```
You misunderstand, the function return type must match. If you want to accept a function pointer that returns Result, you must specify the function type as returning Result. It can't return something else, and still be the same type of function pointer. D unfortunately does not obey covariance or contravariance with function pointer return types, but even if it did, in this case it would not work. -Steve
Jul 04 2023
prev sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
Hopefully we'll get tagged union in the future

- no templates
- proper support
- proper error messages
Jul 04 2023