Eliminate class allocators and deallocators?

Michel Fortin michel.fortin at michelf.com
Thu Oct 8 19:54:31 PDT 2009


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
			...
	}


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list