'unwrap envy' and exceptions

jfondren julian.fondren at gmail.com
Fri Sep 10 15:44:04 UTC 2021


On Friday, 10 September 2021 at 15:26:18 UTC, Kagamin wrote:
> Nullable doesn't replace exceptions, it specifies a value which 
> is legitimately absent, not due to an error

Asking for the maximum of an empty array is an error case (hence 
D's minElement/maxElement thrown an AssertError, not even a 
catchable exception), this is just a boring and expected one, and 
there aren't any other kinds of errors that need to be 
distinguished from it, so it's reasonable to return Option 
instead of Result. Everything works the same for them, Result 
just has more runtime and programmer overhead. Hence also

>>Conclusion: deprecate exceptions, rewrite Phobos to only use 
>>Option and Result sumtypes, and release D3!

rather than "deprecate exceptions, rewrite Phobos to use 
Nullable".

>, it's `Result` that
> replaces exceptions. Also languages built with this pattern in 
> mind have syntax sugar that helps propagate those errors 
> upwards.

Yeah, .assumeWontThrow in a nothrow function is very similar to 
.unwrap, but there's no analogue to ? in a function that just 
propagates errors. Temporarily adding nothrow still tells you 
where exceptions might be coming from, and I suppose a 
Sufficiently Smart IDE could toggle that behind your back to 
annotate uncaught exceptional function calls for you.


More information about the Digitalmars-d mailing list