Retrieving the type by using typeof
Use the typeof
operator to retrieve the run-time type of a data item.
The syntax of the typeof expression is shown below.
typeof expressionThe example below demonstrates
the syntax. The sample typeof
expression evaluates to true.
var myVar : Integer = 10
if (typeof myVar == Integer) print("true") // typeof myVar == Integer
Comparing the retrieved type to a subtype of the retrieved
type evaluates to false.
For example, the Integer
type is a subclass of the Number
class, which itself is a subclass of the Object class. All of the typeof expressions shown below
evaluate to false.
if (typeof myVar == Number) print("false") // typeof myVar != Number
if (typeof myVar == Object) print("false") // typeof myVar != 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
and a NoteSecurityType
typekey. The typeof expressions
shown below evaluate to true.
if (typeof Producer == Type<Producer>) print("true")
if (typeof Producer == Type<entity.Producer>) print("true")
if (typeof NoteSecurityType == Type<NoteSecurityType>) print("true")
if (typeof NoteSecurityType == Type<typekey.NoteSecurityType>) print("true")
The Type<t> syntax must be used
in comparison expressions of entities or typekeys that use the typeof operator. The expressions
shown below evaluate to false
because they do not use the required Type<t>
syntax.
if (typeof Producer == Producer) print("false")
if (typeof Producer == entity.Producer) print("false")
if (typeof NoteSecurityType == NoteSecurityType) print("false")
if (typeof NoteSecurityType == typekey.NoteSecurityType) print("false")
The example below demonstrates how the run-time type
retrieved by the typeof
operator can differ from the type specified when the variable was defined.
var myVar : Object = 10
print(typeof myVar) // Run-time type is java.lang.Integer
The run-time type of a null value is the void type as demonstrated below.
var myVar : Boolean = null
print(typeof myVar) // Run-time type of the null value is void
