Disadvantages of ARC

Adam D. Ruppe destructionator at gmail.com
Thu Feb 6 10:17:23 PST 2014


On Thursday, 6 February 2014 at 17:56:15 UTC, Andrei Alexandrescu 
wrote:
> I will mention again that output ranges lead to quite a bit 
> more code on the caller site.

People are asking for control over memory management. You can't 
then complain that you get control over memory management!

I'd furthermore like to note that there's no reason why we can't 
have the best of both worlds through default parameters and/or 
different names.

Suppose our thing is defined as this:

T[] toUpper(T, OR = GCSink!T)(in T[] data, OR output = OR()) {
     output.start();
     foreach(d; data)
        output.put(d & ~0x20);
     return output.finish();
}

struct GCSink(T) {
     // so this is a reference type
     private struct Impl {
         T[] data;
         void put(T t) { data ~= t; }
         T[] finish() { return data; }
     }
     Impl* impl;
     alias impl this;
     void start() {
         impl = new Impl;
     }
}

// an output range into an existing array container
struct StaticSink(T) {
     T[] container;
     this(T[] c) { container = c; }
     size_t size;
     void start() { size = 0; }
     void put(T t) { container[size++] = t; }
     T[] finish() { return container[0 .. size]; }
}
StaticSink!T staticSink(T)(T[] t) {
     return StaticSink!T(t);
}

void main() {
     import std.stdio;
     writeln(toUpper("cool")); // default: GC
     char[10] buffer;
     auto received = toUpper("cool", staticSink(buffer[])); // 
custom static sink
     assert(buffer.ptr is received.ptr);
     assert(received == "COOL");
}


More information about the Digitalmars-d mailing list