Checking the type of the argument

### 9.2.5 Checking the type of the argument

You can check the type of the argument of a function (or anything else, for that matter) with the type command. For example, entering

type(4)

will return

integer

The output of a type command is actually an integer from 1 to 12. The output of type(4) is integer, which is a constant with the value 1. Another way to represent this type is with DOM_INT; entering

type(4) == DOM_INT

will return

true

Possible types (followed by the integer they represent) include:

- real, double or DOM_FLOAT (1).
- integer or DOM_INT (2).
- complex or DOM_COMPLEX (4).
- identifier or DOM_IDENT (6).
- vector or DOM_LIST (7).
- func or DOM_FUNC (13).
- expression or DOM_SYMBOLIC (8).
- rational or DOM_RAT (10).
- string or DOM_STRING (12).

If the item being tested is a list (in DOM_LIST), then the subtype command can determine what type of list it is. If the object is a sequence, then subtype returns 1;

subtype(1,2,3)

returns

1

If the object is a set, then subtype returns 2. If the object is a polynomial represented as a list (see section 5.25), then subtype will return 10. If the object isn’t one of these types of list, then subtype returns 0.

The compare function will compare two objects taking their type into account; in other words, compare(a,b) returns 1 (true) if a and b have the same type with a less than b, or if a and b have different types and the integer type(a) is less than type(b). For example,

compare("a","b")

returns

1

since "a" and "b" have the same type (string) and "a" is less than "b" in the string ordering. Also, if b is a formal variable, then

compare("a",b)

returns

0

since the type of "a" is string (the integer 12) while the type of b is identifier (the integer 6) and 12 is not less than 6.

The getType command is similar to type in that it takes an object and returns the type, but it has different possible return values. It is included for compatibility reasons. For example,

getType(3.14)

returns

NUM

and

getType(x)

returns

VAR

Other possible return values include STR, EXPR, NONE, PIC, MAT and FUNC.