Type

Type

Type[ expr ] returns the type of an expression, typically to check it in a condition.

Usages

Type[ expr ] == type

Type is mostly used in a comparison, to check what kind of expression a script variable holds. Compare with one of the type names below, or with the name of an operation or function, such as Plus, Sqrt or List.

Type name Matches Example out
NaturalNumber integers, also negative ones Type[-3] == NaturalNumber True
FloatingPointNumber decimal numbers and integers, and constants such as Pi Type[2.5] == FloatingPointNumber True
SimpleFraction fractions of two integers Type[1/2] == SimpleFraction True
Constant all of the above, and True and False Type[Pi] == Constant True
Boolean True and False Type[True] == Boolean True
Variable variables without a value Type[y] == Variable True
Mapping mappings such as x -> x^2 Type[x -> x^2] == Mapping True
Unit units such as 'm' Type['m'] == Unit True
List, Plus, Sqrt, ... expressions with that outer operation Type[x+1] == Plus True
Example in out
A script variable with value π is a constant. x := Pi
r := Type[x] == Constant
r = True
A decimal number is not a natural number. Type[2.5] == NaturalNumber False
A decimal number is not a simple fraction. Type[0.5] == SimpleFraction False
A root is not a constant type: its type is Sqrt. Type[Sqrt[2]] == Constant False

Type[ expr ]

On its own, Type[ expr ] returns the internal type name.

Example in out
An integer. Type[ 35 ] TypeConstantNaturalNumber
A decimal number. Type[ 2.5 ] TypeConstantReal
A sum. Type[ x + 1 ] Plus
A quantity. Type[ Quantity[3,'m'] ] Quantity
Details
  • Use == to compare types. Type[x+1] <> Plus is not evaluated in a script; use it as a condition in If, or use <<>>.
  • Type takes exactly one argument: Type[1, 2] is left unevaluated.

See also: EqualsEval, If, Strict