Validity check before downcasting / exception handling?
markus at gerwinski.de
Sun Mar 18 12:49:11 CET 2001
When downcasting object pointers, I often wish there was a way to check the
Peter has already told, in one of the next alpha releases of GPC there will
be a new downcast-operator named 'as' firing a runtime error in case of an
invalid downcast; like this:
tBar = object ( tFoo ) ...;
with foo^ as tBar do (* if "foo^" isn't a tBar actually, the 'as' *)
... (* interrupts the program with a runtime error. *)
Nevertheless I'd like to have a way to check the validity
_before_ casting, thus preventing the runtime error in cases where I don't
want the program to be interrupted.
Currently, I'd suggest the following additional functions in GPC itself:
Function getObjectType ( obj: object ): "type";
Function isOfObjectType ( obj: object; runtimeType: "type" ): boolean;
p:= new ( pBar, init );
... getObjectType ( p^ ) ... (* returns "tBar" *)
... isOfObjectType ( p^, pFoo ) ... (* returns "true", since tBar *)
(* is derived from tFoo. *)
For downcasting, I'd only need the latter one, but the first function seems
useful to me, too.
Alternatively, I could need a mechanism to absorb the runtime error fired by
"as" before it interrupts the program. Something like this:
with foo^ as tBar do
catchRuntimeError ( @procedureToHandleRuntimeError, parameters );
Anyway, when implementing a catchRuntimeError mechanism in GPC, it would not
seem reasonable to me to just do it for this special kind of runtime error;
thus, here we'd need a general exception handling mechanism to catch and
handle _any_ runtime error without interrupting the program.
Hope this description of my ideas wasn't too confuse...
More information about the Gpc