🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Array trailing comma

Started by
11 comments, last by theoutfield 9 years, 7 months ago

I know it's valid syntax to declare an array like the following

double[] something = {

1.1,

2,2,

2,3,

}

Is it possible to generate a warning or error for the uninitialized value after the trailing comma?

Advertisement

Why would you want that though? It has no adverse effect and just makes it easier to add new elements if you're still working on the code.

I actually use AngelScript for a real time engine test cell controller. One of the users of my application created an array of engine speeds like so

EngineSpeed[] ={

1000,

2000,

3000,

4000,

5000,

6000,

}

The test sequence that was written uses the array size function to loop through each speed. The uninitialized element had a value of x^219 (thankfully engines can't actually go that fast). There should have been a check to see that the speed was within limits but the uninitialized value could have been any valid engine speed as well. I was just looking for a way to issue a warning or error as an additional check to let the user know they made a mistake.

I'm not sure about AngelScript, but languages that permit trailing commas normally make arrays like:


EngineSpeed[] ={
1,
2,
3,
}

...contain 3 elements, not 4 elements with an uninitialized value. Sounds like a bug in AngelScript.

That being said, the bug could be in your code. Here's what you posted:


double[] something = {
1.1, //ONE element. You have a period here, not a comma. Did you mean "1,1" instead of "1.1"
2,2, //Two elements.
2,3, //Two elements.
}
//The array generated should be 5 total {"1.1", "2", "2", "2", "3"}

Is it possible your code is assuming the array size is multiples of two, and so is accidentally accessing one past the end of the array?

The extra comma's were typos. If you execute the following the AngelScript code:

double[] Speed = {
1000,
2000,
3000,
};
Print("Length of Speed = " + Speed.length() + " Speed[3]= " + Speed[3]);

You will get a length of 4 and a random number for index 3. This is what I got on my machine. The value is just a random number.

2014-10-04 4:01:05 PM Length of Speed = 4 Speed[3]= 9.88131e-324

I wish the array would default to a size of 3 and ignore the last value.

This is also valid code and will result in a length of 7.

double[] Speed = {
,
,
1000,
,
2000,
3000,
};
From the AngelScript user guide it indicates this is valid code. I jwould just like the option for a warning or error for my particular case.

When declaring arrays it is possible to define the initial size of the array by passing the length as a parameter to the constructor. The elements can also be individually initialized by specifying an initialization list. Example:

array<int> a; // A zero-length array of integers
array<int> b(3); // An array of integers with 3 elements
array<int> c(3, 1); // An array of integers with 3 elements, all set to 1 by default
array<int> d = {,3,4,}; // An array of integers with 4 elements, where
// the second and third elements are initialized

Oh, I wasn't aware of this. Very strange. I've never used another language with that behavior...sounds like it probably does more harm than good, no? You can always pass null or 0 if you want to have uninitialized elements in your array.

Why would you want that though?

I know why i would.

Not long ago i got a weird crash when loading bytecode containing funcdef array with extra comma. Nothing tried to access last element, couldn't reproduce it, happens only on linux version of application... I'd enable it just to make sure other scripters don't screwup anything - finding single comma in big codebase is not fun at all :]

Games are meant to be created, not played...

Totally understandable -- my comment was based on the assumption that Angelscript was like every other language I've used and that a trailing comma didn't make any difference at all.

I'll add an engine property to allow the application developer to decide how empty elements in initialization lists should be treated by the compiler.

Regards,

Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I've added the engine property asEP_DISALLOW_EMPTY_LIST_ELEMENTS in revision 2050. Set it to true to tell the compiler to give an error on empty elements (but ignore the last comma in lists with variable length).

Thanks,

Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement