Skip to end of banner
Go to start of banner

Type Conversion

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Current »

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.

Casting a structure to a string is equivalent to casting to a string [] and then the resulting array to string.

Calling functions

When calling a function or UDR, if the given parameter's type is not the same as what the function is expecting, the conversion will be done automatically according to the rules above.

function f(string s){
    print(s);
}
f(2); //number automatically converted to string
f(true); // boolean automatically converted to string

Compatibility

These conversions cover the functionality implemented before v2.5, so they are backwards compatible.

Contents

We've encountered an issue exporting this macro. Please try exporting this page again later.
  • No labels