On Mon, 26 Dec 2011 18:25:10 +0100, Andrei Alexandrescu
<seewebsiteforem...@erdani.org> wrote:
Hello,
I've been playing with a new approach to reference counting, in
particular for the containers library.
A small prototype is at http://pastebin.com/WnSQY1Jw. The prototype
features a simple doubly-linked list implementation DListImpl. That is
not supposed to be manipulated directly (or it might, in case the user
wants a simple garbage collected implementation - this is a point in the
discussion).
DListImpl has only a couple of primitives implemented. The only
interesting points that it tries to make are:
(a) the presence of the dispose() primitive, which deallocates all
memory and brings the object back to its .init state
(b) the presence of the dup() primitive, which creates a full-blown
duplicate of the object.
The interesting part of the sample is RefImpl, which has a couple of
quite interesting details:
(a) All interaction with the held object is done via opDispatch. In fact
opDispatch can be engineered to statically enforce no reference to the
held object escapes.
(b) A const/immutable call against a non-existing is silently converted
into a call against a default-initialized object.
(c) If a call works and returns the same type for a non-const and a
const object, then the const version is preferred. This is to reduce the
number of calls to ensureUnique().
Destroy.
Andrei
// "empty" object intended for static and immutable methods when
// the data pointer is null. It won't be modified. We assume the
// empty object is equivalent to a null stored pointer.
private static immutable T _empty;
I don't see much need for this.
Instances without identity are rarely useful, i.e. you will mostly
initialize
them to a particular value before using them.
I could think of some strategy constructs being used with templates,
but then you would not wrap them in RefCounted in the first place.
Of course RefCounted should not create a value when accessing static
members.