dynamicCastable

Functions for class/interface dynamic casting.

These functions don't change type qualifier.

toRawPtr/fromRawPtr should be used to safely convert class instances to raw void* instead of viewAs because interface variables don't point to a beginning of a class instance.

Preconditions: Passed object (pointer for fromRawPtr) is not null.

Note: alias this isn't considered while casting in contrary to build-in cast.

Examples

t
{
	class A { }
	class B { }

	A a = new A;
	Object o = a;
	assert(o.downcast!A is a);
	assert(!o.downcastable!B);
	static assert(!__traits(compiles, a.dynamicCast!B)); // cast impossib
t
{
	interface I { }
	class A : I { }

	A a = new A;
	I i = a;
	void* p = a.toRawPtr;
	// `i` doesn't point to a beginning of a class instance:
	assert(i.viewAs!(void*) != p);
	assert(i.toRawPtr == p);
	assert(p.fromRawPtr!I is a); // `fromRawPtr` is `@syste

Meta