1

我有一个 cfscript 函数,它接收使用 imageNew 创建的图像,并返回一个图像。我如何在声明中指定?到目前为止我有这个:

function image_mutate(imageIn, Array mutations) {

我对 imageIn 使用什么数据类型?血腥无用的文档将其列为“A ColdFusion 图像”,如果我得到元数据,它会将其列为“java.lang.class”,这几乎不是具体的。

4

2 回答 2

6

使用any

function image_mutate(any imageIn, array mutations) {}

您可以使用它来代替任何简单的类型、数组、结构或类。您会在 ColdFusion 中看到很多这样的内容,因为它不是一种强类型语言。

如果您确实需要确保某物是图像,请使用此处isImage()记录的功能。

于 2011-03-10T13:05:55.250 回答
2

返回的实际类型是coldfusion.image.Image。您几乎与 java.lang.Class 在一起——那是 java.lang.Class 的实际实例,它代表了 Coldfusion.image.Image 是什么。要了解我们正在处理哪种类,您需要向 java.lang.Class 询问几个问题:

<cfdump var="#ImageNew()#"/>
<cfdump var="#GetMetaData(ImageNew())#"/>
<cfdump var="#GetMetaData(ImageNew()).getCanonicalName()#"/>
<cfdump var="#GetMetaData(ImageNew()).getName()#"/>
<cfdump var="#GetMetaData(ImageNew()).getSimpleName()#"/>

所以,根据我得到的回复,我尝试了一些场景:

<cffunction name="GetImage" access="private" output="false" returntype="Struct">
    <cfreturn ImageNew()/>
</cffunction>

<cffunction name="GetImage" access="private" output="false" returntype="coldfusion.image.Image">
    <cfreturn ImageNew()/>
</cffunction>

<cffunction name="GetImage" access="private" output="false" returntype="Image">
    <cfreturn ImageNew()/>
</cffunction>

然而,在实践中,它们在运行时都对我失败了:

The value returned from the GetImage function is not of type Struct.
The value returned from the GetImage function is not of type coldfusion.image.Image.
The value returned from the GetImage function is not of type Image.

我认为他们失败的原因是因为 ColdFusion 可能会编译我的代码而没有导入 Coldfusion.image 命名空间。当然,它通过 ImageNew() 使用它,但这可能只是导入像coldfusion.globalFunctions 之类的东西。因此,我的 CFC 不知道 coldfusion.image.Image 到底是什么。

我认为你坚持使用returntype="Any"- 抱歉。我喜欢让我的类型在开发中保持强大,然后在生产中关闭类型检查,所以我听到了。

于 2011-03-25T17:52:55.833 回答