On Sunday, 12 September 2021 at 01:50:37 UTC, H. S. Teoh wrote:
> On Sat, Sep 11, 2021 at 08:13:07PM +0000, Dukc via Digitalmars-d wrote:
> On Friday, 10 September 2021 at 23:20:17 UTC, IGotD- wrote:
> Walter mentioned that exceptions are on the way out because they are expensive and inhibits the optimizer which are fair points. I interpreted this that Walter wants to move away from exceptions in D. Since then I haven't seen any proposal for any new error handling for D.
Well the @nodiscard
proposal, that is very likely to get accepted, is sort-of one. It's basically about returning traditional error values, but forcing the user to be explicit if she really wants to summarily discard them.
Whatever replaces exceptions better be darned good, otherwise I will be very unhappy. Explicit exception handling has its place, but in other cases it just uglifies code for no good reason.
Rust's monadic error handling is frequently ugly, but there's a cultural backing to it; the feature itself doesn't demand all that ugliness. As soon as you change the culture you can have solutions like
// halt program on empty nums
int find_gcd(const int[] nums) nothrow {
import std.numeric : gcd;
import mod.algorithm : minElement, maxElement;
return gcd(nums.minElement.unwrap, nums.maxElement.unwrap);
}
vs.
// return None on empty nums with ? syntax
auto find_gcd(const int[] nums) nothrow {
import std.numeric : gcd;
import mod.algorithm : minElement, maxElement;
return gcd(nums.minElement?, nums.maxElement?);
}
vs.
// throw catchable exception on empty nums
int find_gcd(const int[] nums) {
import std.numeric : gcd;
import mod.algorithm : minElement, maxElement;
return gcd(nums.minElement, nums.maxElement);
}
with the compiler or some alias this
magic inserting code that checks the Option!int returns and throwing on None.
i.e., if you want it you get the explicit error handling, and if you don't want it you get exceptions, and you can guard against unwanted 'helpful' automatic exceptions with nothrow
.