Register based error-handling?
deadalnix via Digitalmars-d
digitalmars-d at puremagic.com
Thu Nov 6 19:39:34 PST 2014
On Thursday, 6 November 2014 at 20:58:02 UTC, Ola Fosheim Grøstad
wrote:
> On Thursday, 6 November 2014 at 20:06:55 UTC, deadalnix wrote:
>> I don't think we should have a language construct for that. It
>> is
>> possible to pack the returned value in a struct that enforce
>> error checking.
>
> How?
>
struct ErrorCodeResult(T) {
// Option 1.
inout(T) get() inout {
if (error_code) throw new Exception("Check error code you
morron !");
return result;
}
// Optin 2.
auto get(alias success, alias failure)() {
if (error_code) return failure(error_code);
return success(result);
}
immutable uint error_code;
private:
T result;
}
The option 3 is to use opDispatch to do the check on everything
and return a ErrorCodeResult of whatever should have been
returned.
Options are numerous for various safety levels, and all as fast
as the manual check.
More information about the Digitalmars-d
mailing list