Eliminate class allocators and deallocators?

Yigal Chripun yigal100 at gmail.com
Fri Oct 9 10:32:11 PDT 2009


On 09/10/2009 04:54, Michel Fortin wrote:
> On 2009-10-08 10:35:01 -0400, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> said:
>
>> I think you'd find this article interesting:
>>
>> http://www.ddj.com/article/printableArticle.jhtml?articleID=184405016&dept_url=/java/
>>
>
> That's
>>
> quite interesting inded. At the end, the author would like a
> Smalltalk-like approach, but believe it's not really possible in a
> static language.
>
> But that's exactly what we can have in D by remaking 'new' as a function
> template. :-)
>
> Just as the author wants, with a template 'new' function it seems quite
> possible to change 'new' into a factory function instanciating the best
> class for the given arguments:
>
> void newGC(T, A...)(A args); // create a garbage-collected instance
>
> String new(T: String)(immutable(char)[] utf8Str) {
> return newGC!UTF8ImmutableString(utf8str);
> }
>
> String new(T: String)(immutable(ubyte)[] strData, int encoding) {
> // instanciate the best string type depending on encoding.
> if (encoding == UTF8)
> return newGC!UTF8ImmutableString(cast(string)strData); else if (encoding
> == ISO_LATIN_1)
> return newGC!OneBytePerCharImmutableString(cast(string)strData, encoding);
> else
> ...
> }
>
>

I like the ruby style syntax option.
I'll also would like the option to encapsulate all the machinery in an 
allocator hierarchy conveniently provided in the stdlib.

my templating skills are weak but is something like this could be possible?

class Foo {
    this(args) {};

    static Foo new(Allocator, ARGS...) (Allocator al, ARGS args) {
       alloc = al;
       return alloc.allocate!Foo(args); // alloc will also call the ctor
    }

    static Foo new(ARGS... args) {
       return GC.allocate!Foo(args);
    }
    Allocator alloc;
}

how does this affect ctors? they could become regular functions.
how does this work with abstract classes?








More information about the Digitalmars-d mailing list