On 2009-10-08 10:35:01 -0400, Andrei Alexandrescu <[email protected]> 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
                        ...
        }


--
Michel Fortin
[email protected]
http://michelf.com/

Reply via email to