applay for template function with sumtypes result?

Steven Schveighoffer schveiguy at gmail.com
Tue Jul 4 15:39:04 UTC 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


More information about the Digitalmars-d-learn mailing list