Is it time for D 3.0?

Jacob Carlborg doob at me.com
Sat Mar 28 08:06:57 UTC 2020


On 2020-03-28 00:40, Adam D. Ruppe wrote:

> It used to be this way! D had a custom exception mechanism (on linux, on 
> Windows it used SEH which works very well) that was quite lightweight, 
> but it got killed in the name of C++ compatibility :(

Funny thing. There's a C++ proposal by Herb Sutter, "Zero-overhead 
deterministic exceptions: Throwing values" [1]. Which more or less 
lowers the current syntax used for exception to something similar to 
return codes.

Existing C++ code like this (which can not afford to use table based 
exceptions):

expected<int, errc> safe_divide(int i, int j) {
     if(j==0)
         return unexpected(arithmetic_errc::divide_by_zero);
     if(i==INT_MIN&&j==-1)
         return unexpected(arithmetic_errc::integer_divide_overflows);
     if(i%j!=0)
         return unexpected(arithmetic_errc::not_integer_division);
     else return i / j;
}

expected<double, errc> caller(double i, double j, double k) {
     auto q = safe_divide(j, k);
     if (q) return i + *q;
     else return q;
}

Can with the new proposal be expressed like this:

int safe_divide(int i, int j) throws {
     if(j==0)
         throw arithmetic_errc::divide_by_zero;
     if(i==INT_MIN&&j==-1)
         throw arithmetic_errc::integer_divide_overflows;
     if(i%j!=0)
         throw arithmetic_errc::not_integer_division;
     else return i / j;
}

double caller(double i, double j, double k) throws {
     return i + safe_divide(j, k);
}

Which are more or less lowered to code similar as the original example.

If we want to stay compatible with C++ exception handling, we need to 
implement this :). Although we currently don't support any other 
features added after C++98.

[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list