RE: [flexcoders] Method variable scoping problem with Flex compiler
Fact of life with ECMAScript (hence, with ActionScript as well). Take a look at the language specification itself for details, but locals in your example are scoped to the function, not to code blocks within the function delimited by braces. Yes, this differs from C-based languages, and as someone who regularly works in both I've been bitten by this more than once as well. Best, Seth From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of toofah_gm Sent: Thursday, December 04, 2008 2:32 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Method variable scoping problem with Flex compiler I came across another scoping issue today that scared me. I sure wish that the scoping in the ActionScript language worked the same way C++, C#, Java, and others work! Here's the code: for each (var o:Object in myArray) { var myProblemObject:ProblemObject; if (some condition) { myProblemObject = new ProblemObject(); ... more code here ... } ... still more code here... if (myProblemObject) { ... do some stuff ... } } In this code myProblemObject is NULL when things start out...great! The problem comes after an instance of myProblemObject gets created. Every time through the loop after this myProblemObject is no longer NULL. This is not consistent with any other language that I have worked with and is therefore not obvious to the developer working with the code. Why is myProblemObject not reset each time through the loop? It is a variable declaration. Gary --- In flexcoders@yahoogroups.com, toofah_gm [EMAIL PROTECTED] wrote: It seems to me and my co-workers that the Flex compiler is broken when it comes to local variable scoping within methods. For example: for (var i:int=0; icount; i++) { // do something } for (var i:int=0; icount; i++) { // do something else } This gives a compiler warning stating that 'i' is already defined. But in every other language that I have used, this is completely valid. Yes 'i' was defined above, but 'i' should only be scoped within the 'for' loop and should be invalid outside of it. Another example: if (x) { var myArray:Array = new Array(); // do more stuff } myArray.push(some data); This one compiles, when I believe that it shouldn't. myArray should only be defined within the 'if' statement. If you don't go into the 'if' statement you have a problem here. Does anyone understand why the Flex compiler allows this? Is this just a BUG with the compiler? Anyway, this is just driving me a little crazy. ;) Gary
Re: [flexcoders] Method variable scoping problem with Flex compiler
*Why* it's different I can't tell you, but if you're being regularly surprised, it sounds like you might benefit from skimming through the language reference. This behavior is described on the Variableshttp://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_07.html#118946page of help resource center, under 'Understanding variable scope'. From there (this may scare you even more!): *ActionScript variables, unlike variables in C++ and Java, do not have block-level scope. ... An interesting implication of the lack of block-level scope is that you can read or write to a variable before it is declared, as long as it is declared before the function ends. This is because of a technique called hoisting, which means that the compiler moves all variable declarations to the top of the function.* On Thu, Dec 4, 2008 at 5:31 PM, toofah_gm [EMAIL PROTECTED] wrote: I came across another scoping issue today that scared me. I sure wish that the scoping in the ActionScript language worked the same way C++, C#, Java, and others work! Here's the code: for each (var o:Object in myArray) { var myProblemObject:ProblemObject; if (some condition) { myProblemObject = new ProblemObject(); ... more code here ... } ... still more code here... if (myProblemObject) { ... do some stuff ... } } In this code myProblemObject is NULL when things start out...great! The problem comes after an instance of myProblemObject gets created. Every time through the loop after this myProblemObject is no longer NULL. This is not consistent with any other language that I have worked with and is therefore not obvious to the developer working with the code. Why is myProblemObject not reset each time through the loop? It is a variable declaration. Gary --- In flexcoders@yahoogroups.com flexcoders%40yahoogroups.com, toofah_gm [EMAIL PROTECTED] wrote: It seems to me and my co-workers that the Flex compiler is broken when it comes to local variable scoping within methods. For example: for (var i:int=0; icount; i++) { // do something } for (var i:int=0; icount; i++) { // do something else } This gives a compiler warning stating that 'i' is already defined. But in every other language that I have used, this is completely valid. Yes 'i' was defined above, but 'i' should only be scoped within the 'for' loop and should be invalid outside of it. Another example: if (x) { var myArray:Array = new Array(); // do more stuff } myArray.push(some data); This one compiles, when I believe that it shouldn't. myArray should only be defined within the 'if' statement. If you don't go into the 'if' statement you have a problem here. Does anyone understand why the Flex compiler allows this? Is this just a BUG with the compiler? Anyway, this is just driving me a little crazy. ;) Gary
Re: [flexcoders] Method variable scoping problem with Flex compiler
AFAIK, It's different because that's the way it works in LISP by way of Scheme, JavaScript, and ECMAScript. -Josh On Fri, Dec 5, 2008 at 12:15 PM, Michael Prescott [EMAIL PROTECTED] wrote: *Why* it's different I can't tell you, but if you're being regularly surprised, it sounds like you might benefit from skimming through the language reference. This behavior is described on the Variableshttp://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_07.html#118946page of help resource center, under 'Understanding variable scope'. From there (this may scare you even more!): *ActionScript variables, unlike variables in C++ and Java, do not have block-level scope. ... An interesting implication of the lack of block-level scope is that you can read or write to a variable before it is declared, as long as it is declared before the function ends. This is because of a technique called hoisting, which means that the compiler moves all variable declarations to the top of the function.* On Thu, Dec 4, 2008 at 5:31 PM, toofah_gm [EMAIL PROTECTED] wrote: I came across another scoping issue today that scared me. I sure wish that the scoping in the ActionScript language worked the same way C++, C#, Java, and others work! Here's the code: for each (var o:Object in myArray) { var myProblemObject:ProblemObject; if (some condition) { myProblemObject = new ProblemObject(); ... more code here ... } ... still more code here... if (myProblemObject) { ... do some stuff ... } } In this code myProblemObject is NULL when things start out...great! The problem comes after an instance of myProblemObject gets created. Every time through the loop after this myProblemObject is no longer NULL. This is not consistent with any other language that I have worked with and is therefore not obvious to the developer working with the code. Why is myProblemObject not reset each time through the loop? It is a variable declaration. Gary --- In flexcoders@yahoogroups.com flexcoders%40yahoogroups.com, toofah_gm [EMAIL PROTECTED] wrote: It seems to me and my co-workers that the Flex compiler is broken when it comes to local variable scoping within methods. For example: for (var i:int=0; icount; i++) { // do something } for (var i:int=0; icount; i++) { // do something else } This gives a compiler warning stating that 'i' is already defined. But in every other language that I have used, this is completely valid. Yes 'i' was defined above, but 'i' should only be scoped within the 'for' loop and should be invalid outside of it. Another example: if (x) { var myArray:Array = new Array(); // do more stuff } myArray.push(some data); This one compiles, when I believe that it shouldn't. myArray should only be defined within the 'if' statement. If you don't go into the 'if' statement you have a problem here. Does anyone understand why the Flex compiler allows this? Is this just a BUG with the compiler? Anyway, this is just driving me a little crazy. ;) Gary -- Therefore, send not to know For whom the bell tolls. It tolls for thee. Like the cut of my jib? Check out my Flex blog! :: Josh 'G-Funk' McDonald :: 0437 221 380 :: [EMAIL PROTECTED] :: http://flex.joshmcdonald.info/ :: http://twitter.com/sophistifunk
Re: [flexcoders] Method variable scoping problem with Flex compiler
This is variable hoisting in action: http://docs.huihoo.com/web/js/es4/core/definitions.html#hoist I can't really find a clearer explanation, but the gist of it is that variable definitions are sometimes moved to the parent block, and sometimes this results in the sort of weird behavior you're seeing. That's the how, but I don't really know the why--anyone more savvy about hoisting care to enlighten the list? -- Maciek Sakrejda Truviso, Inc. http://www.truviso.com -Original Message- From: toofah_gm [EMAIL PROTECTED] Reply-To: flexcoders@yahoogroups.com To: flexcoders@yahoogroups.com Subject: [flexcoders] Method variable scoping problem with Flex compiler Date: Fri, 15 Aug 2008 19:43:09 - It seems to me and my co-workers that the Flex compiler is broken when it comes to local variable scoping within methods. For example: for (var i:int=0; icount; i++) { // do something } for (var i:int=0; icount; i++) { // do something else } This gives a compiler warning stating that 'i' is already defined. But in every other language that I have used, this is completely valid. Yes 'i' was defined above, but 'i' should only be scoped within the 'for' loop and should be invalid outside of it. Another example: if (x) { var myArray:Array = new Array(); // do more stuff } myArray.push(some data); This one compiles, when I believe that it shouldn't. myArray should only be defined within the 'if' statement. If you don't go into the 'if' statement you have a problem here. Does anyone understand why the Flex compiler allows this? Is this just a BUG with the compiler? Anyway, this is just driving me a little crazy. ;) Gary
RE: [flexcoders] Method variable scoping problem with Flex compiler
Not sometimes. Local vars are always hoisted from the block where they are declared to the scope of the entire function. As Ecmascript requires! : ) Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Maciek Sakrejda Sent: Friday, August 15, 2008 12:54 PM To: flexcoders@yahoogroups.com Subject: Re: [flexcoders] Method variable scoping problem with Flex compiler This is variable hoisting in action: http://docs.huihoo.com/web/js/es4/core/definitions.html#hoist http://docs.huihoo.com/web/js/es4/core/definitions.html#hoist I can't really find a clearer explanation, but the gist of it is that variable definitions are sometimes moved to the parent block, and sometimes this results in the sort of weird behavior you're seeing. That's the how, but I don't really know the why--anyone more savvy about hoisting care to enlighten the list? -- Maciek Sakrejda Truviso, Inc. http://www.truviso.com http://www.truviso.com -Original Message- From: toofah_gm [EMAIL PROTECTED] mailto:garym%40byu.edu Reply-To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] Method variable scoping problem with Flex compiler Date: Fri, 15 Aug 2008 19:43:09 - It seems to me and my co-workers that the Flex compiler is broken when it comes to local variable scoping within methods. For example: for (var i:int=0; icount; i++) { // do something } for (var i:int=0; icount; i++) { // do something else } This gives a compiler warning stating that 'i' is already defined. But in every other language that I have used, this is completely valid. Yes 'i' was defined above, but 'i' should only be scoped within the 'for' loop and should be invalid outside of it. Another example: if (x) { var myArray:Array = new Array(); // do more stuff } myArray.push(some data); This one compiles, when I believe that it shouldn't. myArray should only be defined within the 'if' statement. If you don't go into the 'if' statement you have a problem here. Does anyone understand why the Flex compiler allows this? Is this just a BUG with the compiler? Anyway, this is just driving me a little crazy. ;) Gary