[flexcoders] Array literals - odd behavior
In Java and other C-derived languages, an array literal can have a comma after the last element, for example: int[] numList = {1, 2, 3,}; In ActionScript, you can do the same thing: var numList:Array = [1, 2, 3,]; In both cases, the array has a length of 3. The trailing comma is basically ignored, it's permitted to make moving the elements around in the editor easier, I assume. I noticed something else today about array literals in actionscript. You can pretty much add additional commas wherever you want: var numList:Array = [ , , ,1, 2, , 3,,,]; And except for the final trailing comma, the array will have an element for each of those commas. The element will be null or undefined where there is no actual value. I was surprised when I discovered this, because I would swear I've had compiler errors over misplaced commas in array literals before. Am I imagining things? I checked the AS3 programmers reference, and googled around to other pages, and nowhere could I find any documentation of this behavior. Is this a bug, or a feature?
Re: [flexcoders] Array literals - odd behavior
On 11/04/2013 18:30, Dave Glasser wrote: In Java and other C-derived languages, an array literal can have a comma after the last element, for example: int[] numList = {1, 2, 3,}; In ActionScript, you can do the same thing: var numList:Array = [1, 2, 3,]; In both cases, the array has a length of 3. The trailing comma is basically ignored, it's permitted to make moving the elements around in the editor easier, I assume. I noticed something else today about array literals in actionscript. You can pretty much add additional commas wherever you want: var numList:Array = [ , , ,1, 2, , 3,,,]; And except for the final trailing comma, the array will have an element for each of those commas. The element will be null or undefined where there is no actual value. I was surprised when I discovered this, because I would swear I've had compiler errors over misplaced commas in array literals before. Am I imagining things? I checked the AS3 programmers reference, and googled around to other pages, and nowhere could I find any documentation of this behavior. Is this a bug, or a feature? Does it really matter? Nobody in their right mind is going to be populating arrays with multiple commas.
Re: [flexcoders] Array literals - odd behavior
Yes, I think it matters quite a bit. It would be very easy for someone, even in their right mind, to unintentionally have an extra comma somewhere in the middle of a long array literal and not notice it. And the compiler would not tell them, but instead would insert an empty element at that spot. I would prefer that the compiler require you to explicitly indicate each element in an array literal, for example: var myList:Array = [1, 2, 3, undefined, 4, 5, 6]; Or, absent that, I would prefer that the documentation clearly spell out the behavior of extra commas in array literals. From: Paul A. p...@ipauland.com To: flexcoders@yahoogroups.com Sent: Thursday, April 11, 2013 3:00 PM Subject: Re: [flexcoders] Array literals - odd behavior Does it really matter? Nobody in their right mind is going to be populating arrays with multiple commas.
Re: [flexcoders] Array literals - odd behavior
On 11/04/2013 20:45, Dave Glasser wrote: Yes, I think it matters quite a bit. It would be very easy for someone, even in their right mind, to unintentionally have an extra comma somewhere in the middle of a long array literal and not notice it. And the compiler would not tell them, but instead would insert an empty element at that spot. I would prefer that the compiler require you to explicitly indicate each element in an array literal, for example: var myList:Array = [1, 2, 3, undefined, 4, 5, 6]; Or, absent that, I would prefer that the documentation clearly spell out the behavior of extra commas in array literals. It would be nice, but it's hardly essential. It's never going to be good practice to put in multiple commas and I can't ever remember have issues doing it accidentally. Raise a bug. *From:* Paul A. p...@ipauland.com *To:* flexcoders@yahoogroups.com *Sent:* Thursday, April 11, 2013 3:00 PM *Subject:* Re: [flexcoders] Array literals - odd behavior Does it really matter? Nobody in their right mind is going to be populating arrays with multiple commas.
Re: [flexcoders] Array literals - odd behavior
Didn't say it was essential. Just that I would prefer it. As you said, nobody in their right mind would do it. One of the hallmarks of a well-designed language, IMO, is that it doesn't silently allow you to do things that no one in their right mind would do. At least a compiler warning would be nice. I couldn't begin to estimate how much time this compiler warning has saved me: Warning: Assignment within conditional. Did you mean == instead of =? Do I make that mistake frequently? Not really. But if I make it in 1 out of 500 if statements, given the amount of code I crank out on a daily basis, it's going to happen now and then, and the time the compiler warning saves me is significant. From: Paul A. p...@ipauland.com To: flexcoders@yahoogroups.com Sent: Thursday, April 11, 2013 3:52 PM Subject: Re: [flexcoders] Array literals - odd behavior On 11/04/2013 20:45, Dave Glasser wrote: Yes, I think it matters quite a bit. It would be very easy for someone, even in their right mind, to unintentionally have an extra comma somewhere in the middle of a long array literal and not notice it. And the compiler would not tell them, but instead would insert an empty element at that spot. I would prefer that the compiler require you to explicitly indicate each element in an array literal, for example: var myList:Array = [1, 2, 3, undefined, 4, 5, 6]; Or, absent that, I would prefer that the documentation clearly spell out the behavior of extra commas in array literals. It would be nice, but it's hardly essential. It's never going to be good practice to put in multiple commas and I can't ever remember have issues doing it accidentally. Raise a bug. From: Paul A. p...@ipauland.com To: flexcoders@yahoogroups.com Sent: Thursday, April 11, 2013 3:00 PM Subject: Re: [flexcoders] Array literals - odd behavior Does it really matter? Nobody in their right mind is going to be populating arrays with multiple commas.