|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Typeless Expression Evaluation
|
|
|
|
Typelessness in the evaluation of expressions refers to a system's ability to automatically convert between data types in order to satisfy the requirements of the operations in expressions.
ColdFusion has a typeless expression evaluation system that simplifies data manipulation for Web developers. Instead of worrying about compatibility between data types and the conversions from one data type to another, ColdFusion developers can focus on the operations they would like to perform on the data.
|
|
|
|
Operation-driven evaluation |
|
|
|
Traditional programming languages enforce strict rules about mixing different types of objects in expressions. For example, in a language such as Fortran, Pascal, C/C++, or Basic, the expression ("8" * 10) produces some form of compile or run-time error because the multiplication operator expects two numerical operands and "8" is a string. Developers using such languages must constantly worry about converting between data types to ensure error-free program execution. For example, the above expression may have to be written as (ToNumber("8") * 10).
In ColdFusion, however, the expression ("8" * 10) evaluates to the number 80 without generating an error. When ColdFusion processes the multiplication operator, it automatically tries to convert its operands to numbers. Since "8" can be successfully converted to the number 8, the expression evaluates to 80.
|
|
|
|
How ColdFusion processes expressions
|
|
|
In general, ColdFusion processes an expression using the following steps:
- In the case of operators, ColdFusion determines the required operands. For example, the multiplication operator requires its operands to be numbers and the CONTAINS operator requires its operands to be strings. In the case of functions, the required function arguments are determined. For example, the Min function expects two numbers as arguments, and the Len function expects a string.
- In the case of operators, ColdFusion evaluates all operands. In the case of functions, all arguments are evaluated.
- In the case of operators, ColdFusion converts all operands that are of a different type from the required type to the required type. In the case of functions, all arguments that are of a different type from the required type are converted to the required type. (If a conversion fails, and ColdFusion reports an error.)
Because ColdFusion performs automatic conversions based on the operations that are involved in the evaluation of an expression, its typeless expression evaluation mechanism is essentially operation-driven evaluation. Operation-driven evaluation lets ColdFusion developers focus on what they want to do with data, not on the details of ensuring error-free expression evaluation.
|
|
|
|
Conversion between types |
|
|
|
While the typeless expression evaluation mechanism in ColdFusion is very powerful, it cannot perform miracles - not all conversions that seem obvious to a ColdFusion developer can be performed automatically. For example, "eight" * 10 will produce an error since ColdFusion does not convert the string "eight" to the number 8.
While typeless expression evaluation does provide developers with a lot of flexibility at practically no cost, it has its intricacies. It can be helpful to understand the way in which some special values and types -- such as Boolean -- are converted.
The following table explains how conversions are performed. The first column lists the value to be converted. The last four columns list the result of the conversion to a Boolean, a number, a date-and-time value, and a string. Note that complex types, such as arrays, structures, queries, and COM objects, cannot be converted.
Examples of Data Type Conversions
|
Value
|
As Boolean
|
As Number
|
As Date-and-Time
|
As String
|
"YES"
|
TRUE
|
1
|
Error
|
"YES"
|
"NO"
|
FALSE
|
0
|
Error
|
"NO"
|
TRUE
|
TRUE
|
1
|
Error
|
"YES"
|
FALSE
|
FALSE
|
0
|
Error
|
"NO"
|
Number
|
TRUE if Number is not 0, FALSE otherwise
|
Number
|
See Date-and-time values.
|
Number is converted using a default format.
|
String
|
If "YES" or "NO" or if the string can be converted to a number, it is treated just like "Number."
|
If it can be converted to a number, it is.
|
A date-and-time value if String is an ODBC date, time, or timestamp; or if it is expressed in a standard US date or time format, including the use of full or abbreviated month names.
Days of week or unusual punctuation result in error.
Dashes, forward-slashes, and spaces are generally allowed.
|
String
|
Date
|
Error
|
See Date-and-time values.
|
Date
|
Automatic conversion is to ODBC timestamp type.
|
|
|
|
  
|
|
|
AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.
|
|