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 02:37:46 PDT 2017


On Sunday, 21 May 2017 at 09:29:40 UTC, David  Zhang wrote:
> On Sunday, 21 May 2017 at 09:15:56 UTC, Nicholas Wilson wrote:
>> have free functions
>>
>>  Result!(T, ErrorEnum) ok(T)(T t) { return Result(t); }
>>  Result!(T, ErrorEnum) error(T)(ErrorEnum e) { return 
>> Result(e); }
>>
>> then go
>>
>> if (!foo)
>>     return ok(42);
>> else
>>     return error(Error.fooHappened);
>
> Ah, I think you misread. ErrorEnum is a template type, like 
> `T`. There's no ErrorEnum enum specified.

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.


More information about the Digitalmars-d-learn mailing list