Proposal 2: Exceptions and @nogc
Jonathan Marler via Digitalmars-d
digitalmars-d at puremagic.com
Mon Apr 10 14:44:32 PDT 2017
On Monday, 10 April 2017 at 20:52:21 UTC, Lurker wrote:
> Everything looks good except this one line:
>
> On Sunday, 9 April 2017 at 03:26:14 UTC, Walter Bright wrote:
>> throw new E(string);
>>
>
> I don't like it for 2 reasons:
>
> a) E e = new E(string); throw e;
>
> Should be *exactly* the same as
>
> throw new E(string).
>
> I think it's obvious why.
I agree this "seems wrong" at first, but because D does not have
a general solution for reference counted objects, the first case
you show can't be implemented safely. The only reason that
"throw new E()" can be safe is that control is ALWAYS and
IMMEDIATELY given up to the exception handler, so no general
solution is required for reference counted objects because the
only place you need to implement reference counted management is
in the exception handler itself.
Another way to think of it is that this proposal makes "throw
new" into a special operator that is different than composing the
"throw" and "new" operations independently. Once you realize this
it's easy to understand and explain to others as well, i.e.
"throw" operator (throw a Throwable object)
"new" operator (create a GC object)
"throw new" operator (create and throw a reference-counted
Throwable object)
Another way this could be done is instead of overloading "throw
new", you could introduce a new operator like
"throw_ref_counted", but then all code that wants to use
ref-counted exceptions needs to change. I think whether that's
justified is debatable but one argument against it is that
if/when D implements a general solution for ref-counted objects
all the code would need to change AGAIN.
>
> b) Using 'new' in @nogc code feels plain wrong.
>
>
> I think a library method of explicitly creating a ref-counted
> throwable would be better. One can then do
>
> E e = refCountedThrowable!(E)(string);
> throw e;
>
> or, interchangeably,
>
> throw refCountedThrowable!(E)(string);
>
> and
>
> throw new E(string);
>
> would be illegal because new cannot be called from within a
> @nogc method.
>
> (haven't ever used @nogc so I'm probably wrong)
Again since D does not support ref-counted objects in general,
you can't do this. You could discuss how D should implement
general ref-counted objects but that is a HARD problem that's not
likely to be solved soon.
The beauty of this proposal is it's a relatively simple solution
to a BIG problem.
More information about the Digitalmars-d
mailing list