Testing the type by using typeis
A typeis
expression tests the type of an object. If the object is of either the
tested type or a subtype of the tested type, the expression evaluates
to true.
The syntax of a typeis expression is shown below.
OBJECTtypeis TYPEThe definition of a myVar variable of type Integer is shown below. The Integer type is a subclass of
the Number class, which
itself is a subclass of the Object
class. Thus, all the typeis
expressions shown below evaluate to true.
var myVar : Integer = 10
if (myVar typeis Integer) { print("true") } // true because myVar is of type Integer
if (myVar typeis Number) { print("true") } // true because Integer is a subtype of Number
if (myVar typeis Object) { print("true") } // true because Integer is a subtype of Object
The type of an entity or typekey is a parameterized type of
Type<T>.
The following example shows some Type<T> types of a
Producer entity. The Producer class is a subclass of
the BeanBase class. All of the typeis expressions shown
below evaluate to true.
if (Producer typeis Type<Producer>) { print("true") }
if (Producer typeis Type<entity.Producer>) { print("true") }
if (Producer typeis Type<BeanBase>) { print("true") } // true because Producer is a subtype of BeanBase
The following example shows some Type<T> types of a
NoteSecurityType typekey. The NoteSecurityType typekey is
a subclass of the Object class. All of the typeis
expressions shown below evaluate to true.
if (NoteSecurityType typeis Type<NoteSecurityType>) { print("true") }
if (NoteSecurityType typeis Type<typekey.NoteSecurityType>) { print("true") }
if (NoteSecurityType typeis Object) { print("true") }
Some methods, parameters, and return values use the interface type
IType, which is analogous to the parameterized
Type<T>.
If the compiler determines that a particular typeis expression can never
evaluate to true at run time, a compile error is generated. This behavior is
demonstrated in the following example that generates a compile error because the
Boolean variable can never hold an Integer value.
var myVar : Boolean
if (myVar typeis Integer) { print("This message never prints") } // Compile error because Boolean can never be Integer
