Type casting functions.
Functions from this module are intended to be used instead of build-in cast as they provide safer and richer interface.
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.
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 impossible
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 `@system`
Function allowing "blind" casting to any type of the same size.
No operations on actial value performed.
It is just treated as having different type.
These functions don't change type qualifier.
int i = 0; i.viewAs!(ubyte[4]) = 3; assert(i == 0x03030303);