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

David Zhang via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 21 01:44:31 PDT 2017


Hi,

I was reading a bit about this in Rust, and their enum type. I 
was wondering if this is replicate-able in D. What I've got right 
now is rather clunky, and involves using

      `typeof(return).ok` and `typeof(return).error)`.

While that's not too bad, it does involve a lot more typing, and 
thus more area for human error.

If you're not familiar with the Result and Option types, it 
allows you to do something like this:

---
Result!(string, ErrorEnum) someFunction(...)
{
     return Ok("Hello!");
}

Result!(string, ErrorEnum) someFunction2(...)
{
     return Error(ErrorEnum.dummyError);
}
---

I'm not entirely sure it's possible... but I figured I might give 
it a try.


More information about the Digitalmars-d-learn mailing list