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 |
Use the is and is not operators with type names (and special strings, as listed below) to verify a type.
value is number value is not number |
value is string value is not string |
value is list value is not list |
value is hash value is not hash |
value is datetime value is not datetime |
value is duration value is not duration |
value is range value is not range |
value is regex value is not regex |
value is bool value is not bool |
value == null value != null |
value is "callable" value is not "callable" |
Here is an example of using the is operator with a type.
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 is { case number: ... case string: ... }
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, fmt=16) # converts number to a base 16 string
number("FFFF", fmt=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.