Safe reference counting cannot be implemented as a library

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 27 16:02:12 PDT 2015


On Tuesday, 27 October 2015 at 21:50:15 UTC, Andrei Alexandrescu 
wrote:
> On 10/27/2015 05:26 PM, rsw0x wrote:
>> It has been a great success for Rust, I rarely ever see RC 
>> used anywhere
>> in Rust code thanks to borrowing. The new C++ core guidelines 
>> are also
>> heavily based on this cf. *_view types in GSL.
>
> You can safely ignore the C++ part, the views are unsafe. I'd 
> appreciate if you backed up your claim on Rust. -- Andrei

Well, even if the claims for Rust and C++ were both 100% true, 
the fact remains that we need to do something about ref-counting 
in D, because the alternative is the GC, which is not necessarily 
acceptable. We already put lots of stuff on the stack and 
encourage avoiding allocations (so, in that respect, we're 
already doing what it's being suggested C++ and Rust do) - but 
some code simply needs to allocate to do what it does, and while 
the GC actually works fantastically for that in many cases, there 
are cases where it doesn't - particularly when you need 
deterministic destruction. So, even if if ref-counting in D ends 
up being very rare, and even if we don't care about @safety, we 
still need a way to support ref-counting for classes (preferably 
a standard way). And since we do care about @safety, it behooves 
us to at least investigate what the possible solutions are for 
having @safe ref-counting in D (and preferably implement one of 
them) rather than simply accepting that ref-counting can't be 
@safe.

Personally, I think that one of the most important aspects of 
this is finding a way to make it possible to reasonably use 
exceptions in @nogc code. Right now, @nogc is almost synonymous 
with nothrow, which is not at all good IMHO. I do not want us to 
start doing stuff like returning error codes just so that our 
code can be @nogc.

- Jonathan M Davis


More information about the Digitalmars-d mailing list