On 19/04/2015 1:52 p.m., Adam D. Ruppe wrote:
On Sunday, 19 April 2015 at 01:19:47 UTC, Brian Schott wrote:
If you overload a class's `new`, you are deciding for your class's
user how it will be allocated.


Without telling them btw. (Well, except the documentation or the
source.) So now, they new it and expect the GC to clean it up like any
other class.... but that invisibly doesn't happen.

Overriding new was totally a misfeature in D and it should go away.


BTW having new in the language at all I wish wasn't a thing. Perhaps it
was right back in old D1, but not ideal in D2, where we can easily
define a template to do it - which could be easily swapped out at the
usage point and enable all kinds of nice things.

Of course, we can still define library New!T (and indeed, I think we
should), but the keyword will always have a bit of a brainspace edge
over it...

auto adding(int x, int y) {
        int[3] values = allocate!(int[3]);

        values[0] = x;
        values[1] = y;
        values[2] = x + y;

        return values;
}

...

auto adding(int x, int y) {
        int[3] values = new!(int[3]);

        values[0] = x;
        values[1] = y;
        values[2] = x + y;

        return values;
}

...

auto adding(int x, int y) {
        int[3] values = new int[](3);

        values[0] = x;
        values[1] = y;
        values[2] = x + y;

        return values;
}

...

struct MyAllocator {
        T* opAllocate(T)(){
                T* value;

                // alloc

                value.rtInfo.fromGC = false;
                value.rtInfo.isRefCounted = false;
                value.rtInfo.isGCFree = true; // allows GC to free it
                value.rtInfo.freeFunction = &opFree; // maybe?

                return value;
        }

        void opFree(T)(T*){
                // free
        }
}

new(MyAllocator):

auto adding(int x, int y) {
        int[3] values = new int[](3);

        values[0] = x;
        values[1] = y;
        values[2] = x + y;

        return values;
}

...

I'm not totally sold.

Reply via email to