I had an interesting observation about datatype conversion in CF9 when dealing with an error in ORM. I was getting an error that said can't use double for an integer field type (something like that). Now, I know that ColdFusion is datatype "independent". So, all simple variables are of datatype "String". Given that, when we do:
<cfset myVar = 1 /> <cfdump var="#getMetaData(myVar).toString()#" />
The result is "class java.lang.string". Now, if we perform a sum:
<cfset myVar = 1 + 1 /> <cfdump var="#getMetaData(myVar).toString()#" />
The result is "class java.lang.Double". I expected it to be integer. I was assuming that on the Java side they will get converted to integer. So, next I tried:
<cfset myVar = javaCast("int",1) + javaCast("int",1) />
And the result was still "class java.lang.Double".
So, is sum of two integer result in a "Double"? I will let people who are more intelligent than me answer and explain that to me.
But, in the mean time it causes issue when dealing with ORM. I was doing something like this:
var hql = "SELECT IsNull(MAX(sortOrder),0) FROM " & arguments.entity.getEntityName() ; var newSortOrder = ORMExecuteQuery(hql,true) + 1; arguments.entity.setSortOrder(newSortOrder);
This was throwing an error because the datatype of sortOrder field is integer and the type of newSortOrder was "Double". The error can be fixed by using javaCast("int",newSortOrder), but I just changed my select to "IsNull(MAX(sortOrder),0)+1" and it returned me a integer.
Anyone care to explain me why sum of two integer results in a Double in ColdFusion?
Comments
- Ben Nadel
-
Regardless of why ColdFusion switches data types (it is a dynamic language), what I find scary is that the ORM is NOT doing JavaCast() or CFQueryParam/cfsqltype behind the scenes?? That feels very much like a bug.
- January 23, 2010, 6:30 PM
- Reply
- Sumit Verma
-
@Ben
Well I don't see any issue with ORM. ORM is returning the correct type. The problem is in this line:
var newSortOrder = ORMExecuteQuery(hql,true) + 1
because it returns double.
So, back to my original question: Why is sum of two integer returning a "Double" data type. That seems like a bug to me.
- January 24, 2010, 9:23 PM
- Reply



