Coldfusion is a loosely typed language meaning that, unlike many other languages, data types do not have to be typed explicitly. In Coldfusion the variable takes the type of the value that is assigned to it. For example, if a variable is created and set to structNew() that variable is typed as a struct since it has been clearly stated that the variable is to be a new struct.
While you can’t just go around concatenating and performing arithmetic on anything, like concatenating a string to a complex object or adding a number to a letter, Coldfusion will perform data typing changes on the fly for strings and numbers based upon the operation. Let’s take the following for example.
Script Version
component displayname="Data Typing" hint="Indicates various methods of data typing." output="false"
{
public struct function typeData()
displayname="Type Data" description="Types various data." output="false"
{
//create a struct to return our data in
var data = structNew();
//type a number as a string
var numericString = "5";
//type a number as a integer
var numericNumber = 5;
//add numbers
var numbersAdded = numericString + numericNumber; //returns 10
//concatenate numbers
var numbersConcatenated = numericString & numericNumber; //returns "55"
data.numbersAdded = numbersAdded;
data.numbersConcatenated = numbersConcatenated;
return data;
}
}
Tag-based version
<cfcomponent displayname="Data Typing" hint="Indicates various methods of data typing." output="false"> <cffunction name="typeData" displayname="Type Data" description="Types various data." output="false" access="public" returntype="Struct"> <!--- create a struct to return our data in ---> <cfset var data = structNew() /> <!--- type a number as a string ---> <cfset var numericString = "5" /> <!--- type a number as an integer ---> <cfset var numericNumber = 5 /> <!--- add numbers ---> <cfset var numbersAdded = numericString + numericNumber /> <!--- returns 10 ---> <!--- concatenate numbers ---> <cfset var numbersConcatenated = numericString & numericNumber /> <!--- returns "55" ---> <cfset data.numbersAdded = numbersAdded /> <cfset data.numbersConcatenated = numbersConcatenated /> <cfreturn data /> </cffunction> </cfcomponent>
This example shows that if a number and a string containing integers are concatenated it is automatically typed as a string. But if a number and a string containing integers is used in arithmetic the result is automatically typed as a number.
I’ve been using Coldfusion for years now, and I’ve never really thought about how it would handle a simple situation like this. Strongly typed languages would never allow this without using the parseInt() or toString() functions (or whatever the language equivalent of those are). While I have mixed thoughts on this, it is kind of intuitive. I admit that while the rules may not be strictly enforced, I can’t think of a single time that I’ve ended up with a typed integer or typed string on data that caught me off guard. Love it or hate it I think that for those working with Coldfusion this is an important concept to understand. Because, while much data typing is handled implicitly, the programmer must understand that typing is occurring. And the more we can understand how Coldfusion handles typing the better we can control our data.