Escaping the Tyranny of the GC: std.rcstring, first blood

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 23 08:11:39 PDT 2014


On 9/22/14, 11:44 PM, Manu via Digitalmars-d wrote:
> On 23 September 2014 15:37, Andrei Alexandrescu via Digitalmars-d
> The trouble with library types like RefCounted!, is that they appear to
> be conceptually backwards to me.
> RefCounted!T suggests that T is a parameter to RefCounted, ie,
> RefCounted is the significant object, not 'T', which is what I actually
> want. T is just some parameter... I want a ref-counted T, not a T
> RefCounted, if that makes sense.
> When we have T* or T[], we don't lose the 'T'-ness of the object, we're
> just appending a certain type of pointer, and I really think that RC
> should be applied the same way.

That's at most a syntactic issue but not a conceptual one. We have 
things like Array!T and nobody blinks an eye.

> All these library solutions make T into something else, and that has a
> tendency to complicate generic code in my experience. In most cases,
> templates are used to capture some type of thing, but in these
> RefCounted style cases, it's backwards, it effectively obscures the
> type.

As it should. You wouldn't want RefCounted!int to be the same as int*.

> We end out with inevitable code like is(T == RefCounted!U, U) to
> get U from T, which is the thing we typically want to know about, and
> every instance of a template like this must be special-cased; they can't
> be rolled into PointerTarget!T, or other patterns like Unqual!T can't
> affect these cases (not applicable here, but the reliable pattern is
> what I refer to).

It turns out a class type is a good candidate for embedding ref 
countedness in its type; in contrast, RefCounted can be slapped on any 
value type. That's how we plan to make it to work.

> I guess I'm saying, RC should be a type of pointer, not a type of
> thing...

It /is/ a pointer. The longer name for it would be RefCountedPointer. It 
has pointer semantics. If you're looking for pointer syntax as well, 
that would be a bummer.


Andrei


More information about the Digitalmars-d mailing list