Safer casts

Christopher Wright dhasenan at gmail.com
Sun May 11 17:42:17 PDT 2008


Koroskin Denis wrote:
> Use templates. Or use polymorhism, if you expect containers to implement 
> some generic interface.

Problem with using interfaces or base classes is, you can't do the 
following with interfaces, and you can't do it efficiently with base 
classes:

interface Collection(T)
{
	void addRange(U : T)(Collection!(U) collection);
}


While having an abstract class Collection that defines that template 
would make the end users a bit happier, it'd basically boil down to:

void addRange(U : T)(Collection!(U) collection)
{
	this.reserve(collection.length);
	foreach (u; collection) this.add(u);
}



More information about the Digitalmars-d mailing list