Data type compatibility and casting
- Last Updated: March 24, 2021
- 4 minute read
- Corticon
- Version 7.2
- Documentation
An important prerequisite of any comparison or assignment operation is data
type compatibility. In other words, the data type of the equation's LHS (the data type of
A) must be compatible with whatever data type results
from the evaluation of the equation's RHS (the data type of B). For example, if both attributes A and
B are Decimal types, then there will be no problem
assigning the Decimal value of attribute B to attribute
A.
Similarly, a comparison between the LHS and RHS does not make sense unless both
refer to the same kinds of data. How does one compare orange (a String) to July 4, 2014 12:00:00 (a
DateTime)? Or false (a Boolean) to 247.82 (a Decimal)?
In general, the data type of the LHS must match the data type of the RHS before a comparison or assignment can be made. (The exception to this rule is the comparison or assignment of an Integer to a Decimal. A Decimal can safely contain the value of an Integer without using any special casting operations.) Expressions that result in inappropriate data type comparison or assignment should turn red in Studio.
In the examples that follow, the generic Vocabulary from the Rule Language Guide will be used because the generic attribute names indicate their data types:
The following figure shows a set of Action rows that illustrate the importance of data type compatibility in assignment expressions:
Let's examine each of the Action rows to understand why each is valid or invalid.
A—This expression is valid because the data types of the LHS and RHS sides of the equation are compatible. They are both Boolean.
B—This expression is invalid and turns red because the data types of the LHS and RHS sides of the equation are incompatible. The LHS resolves to a DateTime and the RHS resolves to a String.
C—This expression is invalid and turns red because the data types of the LHS and RHS sides of the equation are incompatible. The LHS resolves to a String and the RHS resolves to a DateTime.
D—This expression is valid because the data types of the LHS and RHS sides of the equation are compatible even though they are different! This is an example of the one exception to Corticon's general rule regarding data type compatibility: Decimals can hold Integer values.
E—This expression is invalid and turns red because the data types of the LHS and RHS sides of the equation are incompatible. The LHS resolves to a Boolean, and the RHS resolves to a Decimal.
Note that the Problems window contains explanations for the red text shown in the Rulesheet.
The following figure shows a set of Conditional expressions that illustrate the importance of data type compatibility in comparisons:
Let's examine each of these conditional expressions to understand why each is valid or invalid:
a—This comparison expression is valid because the data
types of the LHS and RHS sides of the equation are compatible. They are both Strings. Note
that Corticon Studio confirms the validity of the
expression by recognizing it as a comparison and automatically entering the values set
{T,F} in the Values column.
b—This comparison expression is invalid because the data
types of the LHS and RHS sides of the equation are incompatible. The LHS resolves to a
String, and the RHS resolves to a DateTime. Note that, in addition to the red text,
Corticon Studio emphasizes the problem by not
entering the values set {T,F} in the
Values column.
c—This comparison expression is invalid because the data types of the LHS and RHS sides of the equation are incompatible. The LHS resolves to a Boolean, and the RHS resolves to a Decimal.
d—This comparison expression is valid because the data types of the LHS and RHS sides of the equation are compatible. This is another example of the one exception to Corticon's general rule regarding data type compatibility: Decimals can be compared to Integer values.
e—This comparison expression is valid because the data types of the LHS and RHS sides of the equation are compatible. Like d, this also illustrates the exception to Corticon's general rule regarding data type compatibility: Decimals can be compared to Integer values. Unlike an assignment, however, whether the Integer and Decimal types occupy the LHS or RHS of a comparison is unimportant.