Class operators are slightly different from the operators above in the sense that they can only be used in class expressions which return a class. There are only 2 class operators, as can be seen in table (12.8).
An expression containing the is operator results in a boolean type. The is operator can only be used with a class reference or a class instance. The usage of this operator is as follows:
This expression is completely equivalent to
If Object is Nil, False will be returned.
The following are examples:
The as operator performs a conditional typecast. It results in an expression that has the type of the class:
This is equivalent to the following statements:
Note that if the object is nil, the as operator does not generate an exception.
The following are some examples of the use of the as operator:
The as and is operators also work on interfaces (both COM and CORBA). They can be used to check whether an interface also implements another interface as in the following example:
Additionally, the is operator can be used to check if a class implements an interface, and the varas operator can be used to typecast an interface back to the class:
Although the interfaces must be COM interfaces, the typecast back to a class will only work if the interface comes from an Object Pascal class. It will not work on interfaces obtained from the system by COM.