开发者

In coldfusion 9 how do I specify that a function returns an image?

I have a cfscript function that takes in an image created with imageNew, and returns an image. How do I specify that in the declaration? I have this so far:

function image_mutate(imageIn, Array mutations) {

What datatype do开发者_高级运维 I use for imageIn? The ever bloody useless documentation lists it as "A ColdFusion image" and if I get the meta data it lists it as "java.lang.class" which is hardly specific.


Use any:

function image_mutate(any imageIn, array mutations) {}

You can use this in place of any simple type, array, structure or class. You'll see lots of this in ColdFusion as it's not a strongly typed language.

If you really need to ensure something is an image then use the isImage() function documented here.


The actual type returned is a coldfusion.image.Image. You were almost there with java.lang.Class -- that's the actual instance of a java.lang.Class which represents what a coldfusion.image.Image is. To find out what kind of class we're dealing with, you need to ask the java.lang.Class a few questions:

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

So, based on the responses I got back, I tried a few scenarios:

<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>

However, in practice, all of them failed on me at runtime:

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.

I think the reason why they failed is because ColdFusion probably compiles my code without importing in the coldfusion.image namespace. Sure, it makes use of it with ImageNew() but that's probably just importing in something like coldfusion.globalFunctions. Thus, my CFC has no idea what a coldfusion.image.Image really is.

I think you're stuck with using returntype="Any" - sorry. I like to keep my types strong for development and then turn off type checking in production, so I hear ya.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜