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.