Smart pointers instead of GC?

Adam D. Ruppe destructionator at gmail.com
Tue Feb 4 20:32:30 PST 2014


On Wednesday, 5 February 2014 at 04:22:21 UTC, Andrei 
Alexandrescu wrote:
> Looks like a major weakness.

Even assuming that's true (something I don't agree with), it 
doesn't apply to D anyway since we can always template these 
functions!


I'd like to say again that multiple pointer types is something we 
already have in D:

* unique pointer (I think this is ~ in Rust but rust syntax looks 
like vomit to me) is achieved in D by disabling postblit with a 
struct and freeing in the dtor (RAII). It CAN offer a release 
method to transfer ownership.

* ref counted pointer is achieved with postblit adding ref, dtor 
releasing ref.

* GC pointer could be as simple as struct GC(T) { @trustme T t; 
alias t this; } (where @trustme disables the escape analysis 
checks I'd like to see implemented. Alternatively, if escape 
analysis was built in, we would make t private than then offer a 
getter property that returns scope T.)

* borrowed pointer is the regular references we have today, plus 
escape analysis.


We don't have to speculate how these work. Just write functions 
that use GC!T or RC!T or Unique!T - types we can go ahead and 
write immediately, they are pretty basic structs. Phobos even has 
unique and refcounted right now. We're already halfway there!


More information about the Digitalmars-d mailing list