Raymond Camden's Blog Rss

Second beta of Railo 3 out

13

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:

view plain print about
1<cfscript>
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.

Comments

[Add Comment] [Subscribe to Comments]

Is the "y" variable also put into the var scope automatically?
No - y is an argument.
@Ray
DUH! <cfgroggy who='gus'/> I haven't finished my first cup of coffee yet!

y is a variable in the argument scope.
I didn't realize this wasn't already in Railo? They have a lot of language configuration stuff in the admin like this which all helps performance. They have a scope cascade setting to make variable lookup faster, settings for Application.cfc to make lookup of that faster on every request and so on.

Good to know about the array / struct shorthand.
Yeah, the stuff in the admin is -very- interesting. I should maybe do a blog entry on these options.
What if x is setup as variable.x within the component and the developer forgets to place variable. before x when it is used within a method.

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?
I just tested this (and btw, you should test this too, it is SUPER trivial to download, unzip, and run railo. No install is required). I added

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)
Right. That's what I thought.

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!
Luckily this works fine with my own personal naming rules.

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.
Hey,

Cool feature, would like to see that in Adobe version.
I've always been curious why ColdFusion doesn't assume that these variables are private / local unless otherwise specified. Anyone know?
It is following the same rules as JavaScript. Consider this block:

<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.
I'm not quite sure I follow the logic of making it match javascript, particularly when it detracts from the whole idea of RAD. The need to var scope everything just requires a lot more typing (and overlooking even one can cause real headaches and wasted time tracking down the problem). I really applaud the folks at Railo because they really seem to have a great feel for adding things to the language that really can save developers a lot of time. Since I have to code for both platforms though, I'll just have to hope these kinds of features make their way into ColdFusion eventually as well!

[Add Comment] [Subscribe to Comments]