The one wrinkle is that you need to wrap string literals "abc" with explicit constructor calls, e.g. RCString("abc"). This puts RCString on a lower footing than built-in strings and makes swapping configurations a tad more difficult.

A few ideas:

import std.traits : isSomeString;
auto refCounted(T)(T value) if (isSomeString!T) {
        static if (is(T == string))
                return new RCXString!(immutable char)(value);
        //....
        static assert(0);
}

static assert("abc".refCounted == "abc");

Wrapper type scenario. May look nicer.

Other which would require a language change of:

struct MyType {
 string value;
 alias value this;

 this(string value) {
  this.value = value;
 }
}

static assert("abc".MyType == "abc");

*shudder* does remind me a little too much of the Jade programming language with its casts like that.

There is one other thing which I don't think people are taking too seriously is my idea of using with statement to swap out e.g. the GC during runtime.

with(myAllocator) {
Foo foo = new Foo; // calls the allocator to assign memory for new instance of Foo
}
// tell allocator to free foo

with(myAllocator) {
Foo foo = new Foo; // calls the allocator to assign memory for new instance of Foo
 myFunc(foo);
}
// if myFunc modifies foo or if myFunc passes foo to another function then:
//  tell GC it has to free it when able to
// otherwise:
//  tell allocator to free foo

class MyAllocator : Allocator {
void opWithIn(string file = __MODULE__, int line = __LINE__, string function = ?) {
  GC.pushAllocator(this);
 }

void opWithOut(string file = __MODULE__, int line = __LINE__, string function = ?) {
  GC.popAllocator();
 }
}

By using the with statement this is possible:
void something() {
 with(new RCAllocator) {
string value = "Hello World!"; // allocates memory via RCAllocator
 } // frees here
}

Reply via email to