Thread overview
applay for template function with sumtypes result?
Jul 04, 2023
kiriakov
Jul 04, 2023
kiriakov
Jul 04, 2023
kiriakov
Jul 04, 2023
ryuukk_
July 04, 2023

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)

July 04, 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:

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

July 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.

-Steve
That's the problem

Result(T) = SumType!(ParsResult!T, Err!T)

July 04, 2023

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

July 04, 2023

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; }
}
July 04, 2023

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

July 04, 2023

Hopefully we'll get tagged union in the future

  • no templates
  • proper support
  • proper error messages