Posted in ColdFusion | Posted on 06-27-2008 | 4,446 views
Just read this on the Railo 3 listserv. The second beta is out. A few new features were added, including the shorthand array/struct support CF8 has and cfdocument/cfpdf support. One really interesting update is the ability to specify where unscoped variables go in a UDF. Consider this example:
2function doit(y) {
3 x = 2;
4 return x*y;
5}
6</cfscript>
7
8<cfoutput>#doit(5)#</cfoutput>
9<cfdump var="#variables#">
As you can see, I have an unscoped X variable. When I dump the Variables scope, I see doit and X. However, I can now jump into the Railo Admin...

when I change the setting to "Always" as you see in the screen shot, now the x variable is automatically placed in the var scope instead.
You can grab the latest build (for all OSes) here.


DUH! <cfgroggy who='gus'/> I haven't finished my first cup of coffee yet!
y is a variable in the argument scope.
Good to know about the array / struct shorthand.
Because, correct me if i'm wrong, but if variable.x is set on the component level then x (without variables.) will refer to the variable scoped x?
Is the engine smart enough to know that x is set on the variable scope level so setting x to a var scope can be bypassed?
cfset varibles.x = "orig"
on top of my script. When I ran the CFM, it correctly considered my X a local var scope variable automatically. So X worked fine in the UDF and did not overwrite my variables.x. So basically with this function on, if you do 'x' it will ALWAYS assume its variable scope.
To me - that's a good thing. If you want to use the variables scope inside a udf or method, it makes sense to properly address it (ie, variables.x)
So it sounds like there may need to be a cleanup strategy that developers may need to take if they happen to not have "variable." before variable scoped variables.
Good to know info. Thanks for testing this Ray and hope things are going well for you and Sean at the new place!
CFMs: Always use full scope for everything but variables scope. So X means variables. And I use url.x for, well, url.x
CFCs: Full scope for everything possible. You can't "scope" var scope stuff, so if I see "x" by itself, it means var scope.
Cool feature, would like to see that in Adobe version.
<script>
function foo() {
x = 1;
return x*2;
}
function goo() { alert(x); }
foo();
goo();
</script>
This will alert 1 showing that x 'leaked' out. But if you var scope x, then goo will have an error because x is not defined.
[Add Comment] [Subscribe to Comments]