Skip to main content
GameDev.net gamedev.net
🔒 Locked

Cast behaviour

Started by mandrav Jan 27, 2011 at 7:20 PM 1 replies 1k views
Original Post
mandrav
mandrav
Hi all,

I have a question about casting null. Currently, casting null raises a script exception:

Something@ s = cast<Something>(null); // <- raises exception


I 'd expect a more C-like approach, like casting null to something just returns null (instead of raising an exception). Is that behaviour intended? Or maybe there's an option somewhere to turn that off?

Just as I was writing this post, I did the following test:



interface I { void test(); }
class A : I { void test(){} }
class B { } // notice that B does NOT implement interface I...

void main()
{
I@ i = A();
B@ b = cast(i);

if (b is null) print("B is null");
else print("B is NOT null");
}


Guess what? The output is "B is null" and there's no exception raised. Exactly what I 'd expect, although if an exception was raised in this case I would also understand it (since "B" has no relation whatsoever to "I").

Thanks,
Yiannis.
WitchLord
WitchLord
It shouldn't raise an exception. null should be castable to any object handle, even though the value would still be null. I'll investigate this.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
mandrav
mandrav
Currently, casting null raises a script exception[/quote]

Hmm, I can't reproduce this anymore. Looks like I stumbled on to this by pure chance... When (and if) I encounter this again I 'll try to isolate the cause.
Sorry for the noise.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.