Rich Rodecker’s blog on flash, flex, actionscript, javascript, and php, with a dash of randomness
Burned by hoisting
A while back I read a post about variable hoisting in Actionscript , and remember thinking how it was weird how that was the first time I'd ever heard about that, and how I'd probably never need to know that if I hadn't until that point anyway. So today I get burned by it. When you declare a variable anywhere within a function, Actionscript 'hoists' that declaration to the top of the function. To quote the original article:
Any variable defined within a function scope, in any structure, at any location, is automatically moved at compile-time to the top of the scope. And it’s accessible anywhere in any of those scopes. In most languages, this would be a compile error.
This is what got me today. In this sample, mainUI and someArray are other properties of the object, but vo is not:
-
override public function execute():void {
-
var container:Canvas = mainUI.getContainer(vo.dataProvider);
-
var dp:ArrayCollection = container.dataProvider;
-
-
for each(var vo:ItemVO in someArray){
-
doStuff(vo);
-
-
}
-
}
As you can see, in the first line of code I try to call vo.dataProvider, but it should not be defined yet. However, since I declare the var 'vo' in the for each loop, the compiler lifted the declaration to the top of the function, which means that as far as the compiler cares, the vo variable existed before my first line of code.
| Print article | This entry was posted by rich on March 18, 2009 at 11:08 pm, and is filed under flash. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
In what way were you "burned"?
You wrote some duff code, used a variable before you declared it, and the compiler fixed it for you?
about 1 year ago
By burned I meant that normally when someone makes a mistake such as that, you'd expect the compiler to catch it...but it didn't "fix it", it let the obvious mistake slide by without any warning or error notices.
about 1 year ago
This is semantics. It appears you are using the word "burned" to imply the compiler was not as helpful as it could be. And true that.
But, if the
voin line 2 was intended to reference a class member rather than the local variable defined in line 5, the more common application of the word "burned" might apply -- the compiler made changes without warning that caused problems. It doesn't sound like that was what happened, though (nor should it, because the compiler would track each variable, in it's appropriate scope, with a unique identifier.That said, whether the compiler promotes the variables or not, it is good practice to a) declare variables before they are used; b) declare variables just before they are used.
It would, however, be helpful to folks less-versed in traditional development practices if a compiler flag enabled warnings about conditions like the one you've mentioned.
Cheers.
about 1 year ago
yes, thanks Eric, you've summed it up nicely.
about 1 year ago
JavaScript actually has a JSLint tool that warns for this and other ECMAScript oddities. I'd love to see that integrated with Flex Builder in addition to getting live feedback while typing about syntactic warnings and errors prior to compilation. As for "hoisting" the next version of ActionScript will provide scoped declaration using the "let" keyword.
about 9 months ago
Eric, the whole point of this post is that AS's "hosting" feature is useless and the compiler ought to warn about usages that represent programming errors 99.9999% of the time. If that's not getting "burned", then what is?
Take your semantic arguments elsewhere, you pusillanimous git.