Type conversion: Class runtime cast

The class runtime cast operator is used to check whether a class does descend from another class. In this manner the ancestry of classes can be verified.

Syntax

(unit‑name::class‑name)class‑operand

Discussion

The class runtime cast operator is a special variant of type conversion suited to verify dynamically whether the class in the operand is equal to or it is derived from the class specified in the operator. If successful, the operator returns the originally specified class operand. If the verification fails, the operator returns a null class.

The desired destination class is specified between a pair of (...) (parentheses) composed of the unit name, the corresponding class is defined inside, and the name of the class itself, both separated by :: (double colon) signs. In the following example, the class runtime cast operator is used to verify whether the class operand c does represent a Text or an Image view class:

var class c;

// Depending on the random value, the variable 'c' will
// refer to a 'Text' or 'Image' view class.
if ( math_rand( 0, 100 ) > 50 )
  c = Views::Text;
else
  c = Views::Image;
  
// What kind of class is 'c'? Try to cast 'c' to 'Views::Text'
var class ct = (Views::Text)c;

if ( ct != null )
  trace "It is a 'Text' view";

// Try to cast 'c' to 'Views::Image' class
var class ci = (Views::Image)c;

if ( ci != null )
  trace "It is an 'Image' view";