Idiomatic way to express errors without resorting to exceptions
Simen Kjærås
simen.kjaras at gmail.com
Mon Mar 9 08:33:40 UTC 2020
On Saturday, 7 March 2020 at 15:44:38 UTC, Arine wrote:
> The case when there isn't a value should be handled explicitly,
> not implicitly. Propogating a None value
> isn't useful
Except when it is useful, and shouldn't be handled explicitly. I
have code in D, C and C++ that looks like this:
ReturnValue result = someInitialValue;
auto foo = getFoo();
if (!foo) return result;
auto bar = foo.fun();
if (!bar) return result;
return bar.gun();
In C#, this would be:
return getFoo()?.fun().gun() ?? someInitialValue;
And with implicit handling in Optional!T, it looks like this:
return getFoo().oc.fun().gun().or(someInitialValue);
Clearly the latter two are more readable, and I'm not gonna care
that it's a little slower in the 99% of cases where speed is not
important.
--
Simen
More information about the Digitalmars-d-learn
mailing list