Conversion Between Data Types
Zim performs conversions and operations between data types in a different manner than other application development languages. The rules for conversions are embedded in Zim itself, and when performing conversions or comparisons, these rules govern how the different data types are handled.
In general, when you write an expression, you can use objects or literals of any data type, in any combination. However, when the software encounters an expression containing values with mixed data types or a data type incompatible with the requirements of the expression, it applies a number of rules for converting the data types should conversion be necessary.
- When values are being compared, VARALPHA and VARCHAR values are treated as if they were ALPHA and CHAR values, respectively.
- When two values of type ALPHA are being compared, an ALPHA-type (case insensitive) comparison is performed.
- When two values of type CHAR are being compared, a CHAR-type (case sensitive) comparison is performed.
- When two values are being compared, one of type CHAR and one of type ALPHA, an ALPHA-type (case insensitive) comparison is performed.
- When two values – only one of which is a number type – are compared or are processed in an arithmetic operation, the value that is not a number type is converted to a number and a numeric comparison or operation is performed.
- If a number (type INT, LONGINT, VASTINT) must be converted to a character string for use by a command or function (e.g., to be output by the LIST command or processed by $ substring), the number is converted and right-justified, always producing a result string long enough to hold the largest possible value for that data type (8 characters for INT; 12 characters for LONGINT; 17 characters for VASTINT).
- Number literals implicitly take the data type VASTINT and assume a number of decimal places equal to the number that actually appear in the literal. If the software has to convert a number literal to a character string for use by a command or function, it becomes a character string 17 characters long, as indicated in Rule 6.
- When two values are being compared, only one of which is type DATE, the other value is converted to DATE (if possible) and a DATE comparison is performed.
- If a number is converted to a date, it is assumed to be in a YYYYMMDD form (e.g., 19991122 is November 22, 1999).
Arithmetic Operations Involving Dates
- When an arithmetic operation is being performed on two values, at least one of which is type DATE, the operation is carried out as summarized below:
|val1 + val2||If only one value is type DATE, the other is converted to a number and added (as a number of days) to the date.
If both values are type DATE, a normal arithmetic operation is performed and the result is treated as a number.
|val1 – val2||If both values are type DATE, the dates are subtracted to yield the number of days between the two dates.
If val1 is type DATE and val2 is not, val2 is converted to a number (if necessary) and is subtracted (as a number of days) from the date.
If val2 is of type DATE and val1 is not, both values are converted to numbers (if necessary) and a normal arithmetic operation is performed.
|Both values are converted to numbers (if necessary) and a normal arithmetic operation is performed.|
- If a DATE value must be converted to a character string, it becomes a string eight characters in length.
Explicit conversion Using Software Functions
Although values are implicitly converted from one data type to another as required, the software provides a number of functions that forcibly convert values from one data type to another.