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, complex, range, bool, string, regex, datetime, duration, list, hash |
The langur is and is not operators are not analogous to the Python operators of the same name. In Python, these operators check whether two variables point to the same object.
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 complex value is not complex |
| 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 fn value is not fn |
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 is { case number, complex: ... # either a number or complex }
switch x { case is number: ... case is not string: ... }
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.