Type names are keywords that can be used to test a type with is and is not operators. Type names can also sometimes be used for type conversion.
type names |
---|
number, range, bool, string, regex, datetime, duration, list, hash |
Here is an example of using the is operator with a type.
val .x = 123 if .x is number: writeln "is a number" if .x is not regex: writeln "not a regex"
The is and is not operators can be used in a switch expression.
switch .x { case is number: ... case is not string: ... }
Checking type with a type name does not throw an exception. Checking type with a string (right-hand operand) may throw an exception if it is not recognized for checking type.
Using the string "callable" (which is not specifically a type), with the is or is not operator tests whether a value is a built-in or user-defined function.
switch .x { case is "callable": ... }
Here are examples of using a type for conversion.
number(dt//) # converts datetime to a number (nanoseconds)
datetime(1234567) # converts nanoseconds to a datetime
string(1234567, 16) # converts number to a base 16 string
number("FFFF", 16) # converts base 16 string to a number
These are just a few examples. The use of type for conversion is covered on the builtins page.