Implicit conversion from 'Ok' to 'Result' type when returning functions

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 21 03:03:58 PDT 2017


On Sunday, 21 May 2017 at 09:55:41 UTC, David  Zhang wrote:
> On Sunday, 21 May 2017 at 09:37:46 UTC, Nicholas Wilson wrote:
>> On Sunday, 21 May 2017 at 09:29:40 UTC, David  Zhang wrote:
>> Well then it becomes
>>  Result!(T, E) ok(T,E)     (T t) { return Result(t); }
>>  Result!(T, E) error(T,E)(E e) { return Result(e); }
>>
>> and then provided it can be inferred (e.g. from the function 
>> signature)
>> it will still work.
>
> But how would it be inferred? Like the `ok` function, `T` could 
> be inferred, but E? I'm not sure I understand. If you have to 
> specify the types every time, it kinda defeats the purpose. 
> With the function signature as it is, you'd have to specify the 
> type of the other type (e.g. you'd need to specify E for 
> `ok()`).

As in the function signature of the function you call `ok` or 
`error` in.

Result!(int, SomeEnum) myfunc(bool foo)
{
     if(!foo)
         return ok(42);
     else
         return error(SomeEnum.fooHappened);
}

should work.


More information about the Digitalmars-d-learn mailing list