Implicit Structure Notation ColdFusion
Published on January 13, 2009
By Pete Freitag
By Pete Freitag
Implicit Structure Notation was added to ColdFusion 8, this allows you to create a struct on the fly in one line.
Here's a simple example of how the new short hand notation works:
<cfset person = { firstname="Pete", lastname="Freitag" }>
In Prior versions we might have done something like this, which is equivalent to the above code:
<cfset person = StructNew()> <cfset person.firstname = "Pete"> <cfset person.lastname = "Freitag">
As you can see the new shortcut notation is very handy!
Implicit Structure Notation ColdFusion was first published on January 13, 2009.
If you like reading about cfml, coldfusion 8, coldfusion, notation, struct, structure, or implicit then you might also like:
- 10 Most Useful Image Functions in ColdFusion 8
- CFImage Effects Library for ColdFusion 8
- CFThread - Don't Abuse It
- Serializing CFC's in ColdFusion 8
The Fixinator Code Security Scanner for ColdFusion & CFML is an easy to use security tool that every CF developer can use. It can also easily integrate into CI for automatic scanning on every commit.
Try Fixinator
CFBreak
The weekly newsletter for the CFML Community
Comments
Forgot to mention, this is running on Adobe CF 9.01
by Erick Wilson on 12/23/2011 at 10:42:57 AM UTC
I know this is an old thread, but let me know if you've ever run into this problem before and know WHY it's doing it.
Put this code in a CFM file and run it to see what it's doing.
<cfscript>
//testMethod(): testing the structure notation of CF
public any function testMethod(required any arg1, required struct arg2, string arg3){
return arguments;
}
goodStruct = StructNew();
goodStruct.var1 = DateFormat(Now(), 'mmm. d, yyyy');
goodStruct.var2 = testMethod(arg1=12, arg2={key1="value1",key2="value2"}, arg3="Good");
goodStruct.var3 = CreateODBCDateTime(Now());
badStruct = StructNew();
if(1 == 1){
badStruct.var1 = DateFormat(Now(), 'mmm. d, yyyy');
badStruct.var2 = testMethod(arg1=12, arg2={key1="value1",key2="value2"}, arg3="Good");
badStruct.var3 = CreateODBCDateTime(Now());
}
</cfscript>
<cfdump var="#goodStruct#" label="Good Struct">
<p>The above struct is NOT created inside an if statement and thus works as expected.<br/></p>
<cfdump var="#badStruct#" label="Bad Struct">
<p>The above struct IS created inside an if statment and notice how the other variables get filled, but variable 1 gets removed from the struct?</p>
<p>We know the IF statement is working because the other 2 variables get created and filled with expected data, but the first variable gets removed from the struct or is never set (my bet is on the former).</p>
Sure would be interested in your take on the situation.
Thanks