Automatic type conversion in ColdFusion 11

I am migrating an application from ColdFusion 9 to ColdFusion 11.

The existing code has a false variable assignment:

 <cfset VARIABLES.roleTypeId = false > 

And then, further, a function that expects this variable to be numeric:

 <cffunction name="rolesForStudy" > <cfargument name="id" hint="Study Id"> <cfargument name="roleTypeId" default="#VARIABLES.roleTypeId#" type="numeric"/> </cffunction> 

I inherited the code, and I can’t protect the original programmer’s solution to configure it this way, but, in short, it worked in ColdFusion 9 and it does not work in ColdFusion 11 (returning a data type error). I assume ColdFusion 9 automatically converted false to 0 .

My question is: is there a configuration parameter that I can change in ColdFusion 11 to do this conversion, like ColdFusion 9? Or will I have to fix this code, as well as probably many other similar examples throughout the application? Neither I nor our ColdFusion administrator could find any information about this in the ColdFusion admin interface, the ColdFusion documentation, or the web.

Edit in response to Adam Cameron in the comments.

I created a file that consists of the following 10 lines (and nothing more):

 <cfset VARIABLES.roleTypeId = false > <cfoutput> <p>#rolesForStudy( 1, VARIABLES.roleTypeId )#</p> </cfoutput> <cffunction name="rolesForStudy" > <cfargument name="id" hint="Study Id"> <cfargument name="roleTypeId" default="#VARIABLES.roleTypeId#" type="numeric"/> <cfreturn "It worked" > </cffunction> 

When I run it in ColdFusion 9, it displays the words "It worked."

when I run it in ColdFusion 11, it returns the following error message:
If the component name is specified as the type of this argument, it is possible that the definition file for the component cannot be found or is not available.

+5
source share
3 answers

I believe that you will have to fix the code. There are no settings (which I know, anyway) that change the way CF handles boolean types. You should be able to change the assignment above from "false" to 0, and your function code will work. However, I suspect that elsewhere you might have something like <cfif variables.roletypeID IS "False"> , which will then be broken, because it really looks for a string that also works (ha). CF processing of multiple values ​​in the form of boolean (0, not 0, true, false, yes, and no) is a legacy of its origin. This is convenient at times, but definitely leads to such things.

Meanwhile, I wonder if this behavior change is a new bug or a fix for an old bug. In my opinion, passing “false” as an argument and reading it as a numeric one seems inconsistent, so the new way to do this seems right to me. However, many many languages ​​consider 0 or not 0 as the default values ​​for true and false.

EDIT:

According to Adam's comments below, my code example is where someone would say:

 <cfif somevar IS "false"> 

... will work even if somevar is really numeric. His example (useful) is as follows:

 #0 is "False"# 

... will output “yes” - therefore CF puts the string “false” to zero under the hood for comparison. This makes my example wrong.

My answer is still correct, I believe. The problem he is facing is that the argument is passed to his function - the presence of the type "boolean" causes an error because the function expects a numeric value. But Adam's point and example make me think that maybe this behavior is a mistake - because apparently CF does not discard the number before it checks the type (something that it did in CF 9 according to Joe) .

+7
source

NumberFormat is your savior, which will be this comment in one of Ben Nadel’s articles.

 <cfset VARIABLES.roleTypeId = false> #NumberFormat(VARIABLES.roleTypeId)# <!-- = 0 --> <cfset VARIABLES.roleTypeId = true> #NumberFormat(VARIABLES.roleTypeId)# <!-- = 1 --> 

So, you should be able to either convert it to the expected numeric type before calling the function, or just do

 <cfargument name="roleTypeId" default="#NumberFormat(VARIABLES.roleTypeId)#" type="numeric"> 
+3
source

I raised this as an error in ColdFusion 11: " A coercion type failure while passing boolean to numeric arg ."

I recommend you get around this by removing the type from the argument. This is probably the smallest influence.

This is also a rare case when I add a comment to the code explaining why you turned off type checking.

Apologized to Joe, Mark, and Duncan for resolutely contradicting what they found. However, I do not agree that their answers are the best approach here; -)

+1
source

Source: https://habr.com/ru/post/1208894/


All Articles