自动类型转换在ColdFusion 11 [英] Automatic Type Conversion in ColdFusion 11

查看:142
本文介绍了自动类型转换在ColdFusion 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个应用程序从ColdFusion 9迁移到ColdFusion 11。



现有代码中有一个变量赋值给 false

 < cfset VARIABLES.roleTypeId = false> 

然后,更远一些的函数,期望这个变量是数字:

 < cffunction name =rolesForStudy> 
< cfargument name =idhint =Study Id>
< cfargument name =roleTypeIddefault =#VARIABLES.roleTypeId#type =numeric/>
< / cffunction>

我继承了代码,我不能保护原始程序员的决定,但总之,它在ColdFusion 9中工作,并且在ColdFusion 11中不能工作(返回数据类型错误)。我假设ColdFusion 9自动将 false 转换为 0



我的问题:是否有一个配置设置,我可以在ColdFusion 11中更改,使其像ColdFusion 9那样进行转换?或者我将不得不修复这个代码,以及可能许多其他类似的例子在整个应用程序?我和ColdFusion管理员都没有在ColdFusion管理员界面,ColdFusion文档或在线上找到任何有关此信息。



编辑响应Adam Cameron在评论中



我已经创建了一个由以下10行(而不是其他)组成的文件:

 < cfset VARIABLES.roleTypeId = false> 
< cfoutput>
< p> #rolesForStudy(1,VARIABLES.roleTypeId)#< / p>
< / cfoutput>

< cffunction name =rolesForStudy>
< cfargument name =idhint =Study Id>
< cfargument name =roleTypeIddefault =#VARIABLES.roleTypeId#type =numeric/>
< cfreturnIt working>
< / cffunction>

当我在ColdFusion 9中执行它时,会显示It works

当我在ColdFusion 11中执行它时,它返回以下错误消息:

如果组件名称指定为此参数的类型,

解决方案

我相信你将必须修复代码。没有任何设置(我知道在任何速度),改变了CF处理布尔类型的方式。您应该能够将上面的assignement从false更改为0,您的功能代码将工作。但我怀疑在其他地方你可能有像< cfif variables.roletypeID ISFalse> ,然后会被打破,因为它是真正的寻找一个字符串 - - 工程(ha)。 CF将无数值作为布尔值(0或不为0,true,false,yes和no)处理是它的起源。这很方便,但肯定会导致像这样的事情。



同时我不知道这种行为的改变是一个新的错误或修复一个旧的bug。在我看来,传递假作为参数,它读作数字似乎不一致,所以新的这样做的方式似乎对我来说。然而,许多语言将0或不为0作为true和false的默认值。



编辑:



根据下面的Adam的评论,我的代码示例,有人会说:

 < cfif somevar ISfalse 

...即使somevar确实是数字也会工作。他的例子(有用)是:

 #0是False#

...将输出yes - 所以CF重写字符串false为零下比较。这使我的例子不正确。



我的答案仍然正确我相信。他遇到的问题是传递给他的函数的参数 - boolean类型是抛出一个错误,因为函数期望一个数值。但是亚当的观点和例子让我想到这种行为是一个错误 - 因为它似乎CF不是铸造一个数字之前checkign类型(根据Joe在CF 9中做的)。


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

In the existing code there is an assignment of a variable to false:

<cfset VARIABLES.roleTypeId = false >

And then, farther down, 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 cannot defend the original programmer's decision to set it up this way -- but, in short, it worked in ColdFusion 9, and it doesn't work in ColdFusion 11 (returning a data type error). I assume that ColdFusion 9 was automatically converting false to 0.

My question: Is there a configuration setting that I can change in ColdFusion 11 to make it do the conversion like ColdFusion 9 did? Or will I have to fix this code, along with probably lots of other similar examples throughout the application? Neither I nor our ColdFusion administrator has been able to find any information about this in the ColdFusion Administrator interface, the ColdFusion documentation, or online.

Edit in Response to Adam Cameron in comments

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

<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 execute it in ColdFusion 9, it displays the words "It worked".

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

解决方案

I believe you will have to fix the code. There are no settings (that I know of at any rate) that alter the way CF handles boolean types. You should be able to change the assignement above from "false" to 0 and your function code will work. However I suspect elsewhere you might have something like <cfif variables.roletypeID IS "False"> which will then be broken as it is in truth looking for a string - which also works (ha). CF's handling of a myriad of values as boolean (0 or not 0, true, false, yes and no) is a legacy of it's origin. It's convenient at times but definitely leads to things like this.

Meanwhile I wonder if this change of behavior is a new bug or the fixing of an old bug. In my view passing "false" as an argument and having it read as numeric seems inconsistent so the new way of doing it seems right to me. However, many many languages treat 0 or not 0 as default values for true and false.

EDIT:

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

<cfif somevar IS "false">

...would work even if somevar was indeed numeric. His example (useful) is that:

#0 is "False"#

...will output "yes" - so CF is recasting the string "false" to a zero under the hood for comparison. That makes my example incorrect.

My answer is still correct I believe. The issue he's running into is that the argument passed to his function - being of the type "boolean" is throwing an error because the function expects a numeric value. But Adam's point and example makes me think perhaps this behavior is a bug - since it appears CF is not casting to a number before checkign the type (something it did do in CF 9 according to the Joe).

这篇关于自动类型转换在ColdFusion 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆