%TypeOf

This special method returns a string describing the type of an object, field, or any other element:

All special method names and keywords are case-insensitive.

Syntax

%TypeOf(Element, expression)

%TypeOf()

Syntax elements

expressionAn expression that evaluates to an internal object, field, or variable.

Applicability

Internal objects only.

Comments

The Element keyword can in fact be anything, but something is required to distinguish this form of the method from the parameterless form. Attempting to get the type of a simple variable returns "NAO" (not an object).

%TypeOf() can be used only after accessing an object. Attempting to use it a second time, without accessing an object, causes an exception.

Examples

Example 1

Get the types of fields and objects directly:

Connection = %ObjMgr()
*
Manifest = %New()
Manifest->Shipment = %Array()
Manifest->Shipment(0) = 2021
Manifest->Shipment(1) = "Amsterdam"
Manifest->Shipment(2) = %Null()
Manifest->Shipment(3) = (bool)@TRUE
Manifest->Shipment(4) = (bool)@FALSE
*
FOR I = 0 TO 4
   LineItem = %TypeOf(Element, Manifest->Shipment(I))
   PRINT  "Shipment ":I:" type = ":LineItem
NEXT
*
PRINT "Connection type = ":%TypeOf(Element, Connection)
PRINT "Manifest   type = ":%TypeOf(Element, Manifest)
PRINT "Shipment   type = ":%TypeOf(Element, Manifest->Shipment)
PRINT "LineItem   type = ":%TypeOf(Element, LineItem)
*
Consignments = %New(JSON,"[123,456,789]")
Displacement = %New(JSON,'{"Tonnage":45000}')
*
PRINT "Consignments type = ":%TypeOf(Object, Consignments)
PRINT "Displacement type = ":%TypeOf(Object, Displacement)

which produces:

Shipment 0 type = number
Shipment 1 type = string
Shipment 2 type = null
Shipment 3 type = boolean
Shipment 4 type = boolean
Connection type = objmgr
Manifest   type = object
Shipment   type = array
LineItem   type = NAO
Consignments type = array
Displacement type = object

Example 2

Given the definition of Manifest in the previous example, gets the type of the element last accessed:

*
FOR I = 0 TO 4
   Detail = Manifest->Shipment(I)
   LineItem = %TypeOf()
   PRINT  "Shipment ":I:" type = ":LineItem
NEXT
*

which produces:

Shipment 0 type = number
Shipment 1 type = string
Shipment 2 type = null
Shipment 3 type = boolean
Shipment 4 type = boolean

See also

%Array, %New, %Null, %Type