Take the follwing example:
class Foo
{
}
class Bar : Foo
{
void Test()
{
print("Test was called");
}
}
void BugTest()
{
ref@ x = Foo();
Bar@ y = cast<Bar>(x);
if (y is null)
print("y is null, as expected");
else
{
print("y is not null, calling Test() now:");
y.Test();
}
}
Here, the explicit cast to Bar should return null as expected, but it doesn't, because it only checks whether the types are derived from each other. Calling y.Test() can even cause a crash here.
If you explicitly cast the ref to Foo first before casting that to Bar, it will return null properly.
↧