2011-03-10 24 views
1

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

function image_mutate(imageIn, Array mutations) { 

什么数据类型用于imageIn?血腥无用的文档将其列为“ColdFusion图像”,如果我获取元数据,则会将其列为“java.lang.class”,这很难具体。

+0

类型检查真的很重要吗?为什么不'type =“任何”'? – orangepips 2011-03-10 10:56:19

回答

6

使用any

function image_mutate(any imageIn, array mutations) {} 

您可以在任何地方简单类型,数组,结构或类的使用。您将在ColdFusion中看到很多这样的内容,因为它不是强类型语言。

如果你真的需要确保东西是图像然后使用isImage()功能记录here

+0

+1 is'mage()' – orangepips 2011-03-10 14:15:12

+0

感谢您的支持。我遇到过“任何”,但我来自强类型语言,但感觉不对。我只需要习惯它比我习惯的宽松一点。非常感谢isImage()。 – boodle 2011-03-12 09:11:04

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" - 抱歉。我喜欢保持我的类型强大的发展,然后关闭生产中的类型检查,所以我听到你。