Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
General Rules
Type conversion is usually done when using the attrib (=) operator between variables of different types. This page refers to the automatic type conversion, and not the one you obtain using the cast operator.
The types convertible to a target type are listed in the table below:
Target Type | Convertible types | Additional Information / Examples |
---|---|---|
number | string, integer | "1" to 1 1 to 1.0 |
integer | string, number | "2" to 2 2.0 to 2 |
string | ALL | |
boolean | string, number, integer | "true" to true value of number != 0 |
date | string, number, integer | Constructing a date from number assumes the number is a "to millis" representation of a date |
interval | string, number, integer | Constructing a date from number assumes the number is a "to millis" representation of an interval 60000 to "1m" "120m" (string) to "2h" (interval) |
When converting a string to an array type, the string is first transformed to a string[] by splitting it at every "|" character, and then the conversion is done from string [] to the target type array.
The conversion from string to a target type is done by parsing the string as a text representation of the target type.
Converting Arrays
Converting arrays of different types is possible if the element types (inner type of the array) are convertible according to the the rules explained on this page. For example you can cast a number[] to a string[] because number is convertible to string, however you cannot convert a date[] to an interval[] because date cannot be converted to interval.
Converting Structures
Structures can only be converted directly to string or an array type. Converting a structure to an array requires that each field of the structure is convertible to the element type (inner type) of the array.
It may be possible for certain structures to be convertible to other types by using one or more intermediate types, however this is not done implicitly and users are required to use explicit casting in such cases. For example, a structure containing a single number field may be converted to a number by first casting it to a string then the resulting string to a number.
Tip |
---|
Casting a structure to a string is equivalent to casting to a string [] and then the resulting array to string. |
Tip |
---|
Calling routinesWhen calling a routine or UDR, if the given parameter's type is not the same as what the routine is expecting, the conversion will be done automatically according to the rules above. |
Code Block |
---|
function f(string s){ print(s); } f(2); //number automatically converted to string f(true); // boolean automatically converted to string |
Info |
---|
CompatibilityThese conversions cover the functionality implemented before v2.5, so they are backwards compatible. |
Contents
Table of Contents |
---|