10.17 Type Conversions

The syntax for the Report Writer is completely typeless. This does not mean that data types are ignored by the Report Writer. Rather, it means that all data types are dynamically implied. Every field, variable and parameter has associated with it a value, a natural type and a current type.

The natural type is the type of data the field, variable or parameter should contain. The current type is the type of data currently being stored in the field, variable or parameter.

The natural type of a variable is REAL, the natural type for a field is the type listed in the data dictionary, and the natural type of a parameter is a STRING type. Current types and natural types can differ only by making assignments. For example:

A field has a natural type of REAL. If you assign the field a value of "40.54", the field will then have a current type of STRING. If the file was to be INSERTed or UPDATEd, the current value of the field would be converted to a REAL before the record was stored.

The concept of natural types and current types is necessary for file reading. When reading a file, it is necessary to know how to pick up the data from the record. After each record is read, all the fields of the file read will have natural types. All other operations deal only with current types.

Various operators will require certain types as operands, others will produce certain types as their result. For this reason, you must consider what the operand types are for each operation and what the resulting type will be for that combination of types. Most of the time this will be perfectly natural. For instance: MONEY type times a REAL type will yield a MONEY type (i.e., price per unit * #units = total cost).

Before each operator is applied to its operands, the operands undergo some modifications to make the operator work in a sensible way. The following summarizes, by operator, what changes are made:

minus (-), plus (+):

gifs/20000000.gif If either operand is type MONEY, both types are forced to MONEY.

gifs/20000000.gif Else if either operand is type BOOLEAN, both types are forced to BOOLEAN (plus only).

gifs/20000000.gif Else if an operand is a string in a valid date format, the string is made into type DATE.

gifs/20000000.gif Else if an operand is a string containing a colon ':', try to convert the string into type TIME.

gifs/20000000.gif Else the operands are made into type REAL.

times (*):

gifs/20000000.gif If either operand is type MONEY, both types are forced to MONEY.

gifs/20000000.gif Else if either operand is type BOOLEAN, both types are forced to BOOLEAN.

gifs/20000000.gif Else the operands are made into type REAL.

divide (/), integer divide (DIV), and modulo (%):

gifs/20000000.gif If an operand is not of type MONEY, it is converted to type REAL.

relational operators (<= < <> = > >=):

gifs/20000000.gif If either operand is of type MONEY, then both operands are made into type MONEY.

gifs/20000000.gif Else if either operand is of type DATE, then both operands are made into type DATE.

gifs/20000000.gif Else if either operand is of type TIME, then both operands are made into type TIME.

gifs/20000000.gif Else if either operand is of type BOOLEAN, then both operands are made into type BOOLEAN.

gifs/20000000.gif Else if both operands are not of type CHAR or STRING, then they are both made into type REAL.

logical operators (AND, OR, NOT):

gifs/20000000.gif Convert both operands to type REAL.

After each operator has been applied, the type of the result is determined by the types of the two operands. The following tables define the result types of each operator. To find a result type, first scan across the top of the table to find the type of the first operand. Then scan down that column until you reach the row marked with the type of the second operand. The letter in this entry is the result type of the operator. The letter abbreviations for the types are:

b boolean types

c char or string types

d date types

I integer types

m money types

r real types

t time types

If an entry has the value of "n", then the resulting type of that operator with those types has no meaning. For example: money times money has no meaning, hence the entry is 'n'. If you do create an nonsense expression (money times money), your result will be of type REAL, and the Report Writer will attempt to make the result reasonable.

plus (+) | b c d i m r t

------------------------------------------------------------------

b | b b n b n b n

c | b r d i m r t

d | n d n d n d n

i | b i d i m r t

m | n m n m m m n

r | b r d r m r t

t | n t n t n t n

minus (-) | b c d i m r t

------------------------------------------------------------------

b | n n n n n n n

c | n r d r m r t

d | n n r n n n n

i | n r d i m r t

m | n m n m m m n

r | n r d r m r t

t | n n n n n n r

times (*) | b c d i m r t

------------------------------------------------------------------

b | b b n b n b n

c | b r n r m r n

d | n n n n n n n

i | b r n i m r n

m | n m n m n m n

r | b r n r m r n

t | n n n n n n n

divide (/) | b c d i m r t

------------------------------------------------------------------

b | n n n n n n n

c | n r n r m r n

d | n n n n n n n

i | n r n r m r n

m | n n n n r n n

r | n r n r m r n

t | n n n n n n n

divide (DIV) | b c d i m r t

------------------------------------------------------------------

b | n n n n n n n

c | n r n i m r n

d | n n n n n n n

i | n r n i m r n

m | n n n n n n n

r | n r n i m r n

t | n n n n n n n

modulo (%) | b c d i m r t

------------------------------------------------------------------

b | n n n n n n n

c | n r n r m r n

d | n n n n n n n

i | n r n r m r n

m | n n n n n n n

r | n r n r m r n

t | n n n n n n n

exponent (**) | b c d i m r t

------------------------------------------------------------------

b | n n n n n n n

c | n r n r m r n

d | n n n n n n n

i | n r n r m r n

m | n n n n n n n

r | n r n r m r n

t | n n n n n n n

relation | b c d i m r t

------------------------------------------------------------------

b | b n n n n n n

c | n b n b b b n

d | n b b n n n n

i | n b n b b b n

m | n b n b b b n

r | n b n b b b n

t | n b n n n n b

uminus | b c d i m r t

------------------------------------------------------------------

| b r n i m r n