digitalmars.D.learn - applay for template function with sumtypes result?
- kiriakov (25/25) Jul 03 2023 Hi. I can't handle template. It all looks sad, build ok, test
- Steven Schveighoffer (20/52) Jul 04 2023 If inference isn't working you can explicitly instantiate the template
- kiriakov (4/8) Jul 04 2023 That's the problem
- Steven Schveighoffer (5/15) Jul 04 2023 No, `Result(T)` is its own struct in your example.
- kiriakov (11/27) Jul 04 2023 I was recommended:
- Steven Schveighoffer (8/36) Jul 04 2023 You misunderstand, the function return type must match. If you want to
- ryuukk_ (4/4) Jul 04 2023 Hopefully we'll get tagged union in the future
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
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
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`. -SteveThat's the problem Result(T) = SumType!(ParsResult!T, Err!T)
Jul 04 2023
On 7/4/23 10:19 AM, kiriakov wrote:On Tuesday, 4 July 2023 at 12:43:19 UTC, Steven Schveighoffer wrote: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. -SteveOn 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)
Jul 04 2023
On Tuesday, 4 July 2023 at 14:42:31 UTC, Steven Schveighoffer wrote:On 7/4/23 10:19 AM, kiriakov wrote: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; } } ```On Tuesday, 4 July 2023 at 12:43:19 UTC, Steven Schveighoffer wrote: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. -SteveOn 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)
Jul 04 2023
On 7/4/23 11:15 AM, kiriakov wrote:On Tuesday, 4 July 2023 at 14:42:31 UTC, Steven Schveighoffer wrote: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. -SteveOn 7/4/23 10:19 AM, kiriakov wrote: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; } } ```On Tuesday, 4 July 2023 at 12:43:19 UTC, Steven Schveighoffer wrote: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.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)
Jul 04 2023
Hopefully we'll get tagged union in the future - no templates - proper support - proper error messages
Jul 04 2023