Raymond Camden's Blog Rss

Implicit array/struct and function calls

3

Posted in ColdFusion | Posted on 12-27-2007 | 2,092 views

I'm thinking this is something folks have blogged about before, but I just ran into it today. When using array and struct implicit creation, you cannot use the syntax directly in a udf/method call. Let me explain. Consider this example:

view plain print about
1<cfset z = {name='r',age=34}>
2<cfset somefunc(data=z)>

In this example I use shorthand notation to creation a structure. I then pass it to somefunc as an argument named data. This works just fine. But this version, which should be the same, will give you a syntax error:

view plain print about
1<cfset somefunc(data={name='r',age=34})>

I also tried the simpler form:

view plain print about
1<cfset somefunc({name='r',age=34})>

Which also failed. It looks like you can only use the implicit creation in cfset statements. (Which is certainly ok, I'm just happy we have this shortcut!)

Comments

[Add Comment] [Subscribe to Comments]

I saw Sean and Dan Wilson talking about a similar subject the other day. I didn't realize we had an easier way to set up structures.

Good thing to know.
The same is true of cfparam:

<cfparam name="test" default="#[1, 2, 3, 4]#">
<cfdump var="#test#">

I've also seen it flake out another way but can't remember how. Annoying but not the end of the world.
I get this everytime I try to return struct from function.
You can't do:
<cfreturn {field=3, field2=true}>
you have to assign it to var first.
Seems really odd that it does not work.