I would like to find a way I can create an initialized dynamic size array. It shouldn't be using loops of course. I thought maybe overriding the base array class with somehow to pass to parameters 1) a number for repeat and 2) item as the initializer. And when a user requests at item which is not defined the overriding class will return the item instead of undefined or exception. is this possible in a safe way?
By default value of all elements of the newly created instance of the Array class are "undefine" . In many cases checking for value “undefined” when access an element of array is a waste of time. In order to remove this check the array should have valid items. So there are two ways to do that:
The first way is to iterate all elements in array and initialize it ( initializing can be setting a value or creating a instance of some class ). In this way all elements of the array will contain a valid data.
Second way is to extend Array class and check for the "undefine" when someone access the element. In this way the array may contain an undefined elements but if someone tries to access it they will be initialized. Unfortunately this way cannot be applied here because in AS3 operators
cannot be override ( we need to override operator [] ) and functions cannot be overloaded ( we need a constructor with two parameters - size and type. We can use second constructor of the Array class and handle the situation when instance is created with two parameters and the second parameter is a
type ).
So the solution is to implement the first way with a function that will iterate all elements of the array and initialize it. The iteration can be explicit ( we will write the loop ) or implicit( using forEach method of Array class )
In matter of facts there is a way to override operator [] but for this our class must inherit Proxy class and we must implement all methods of the Array class. Moreover because this class will not extends Array class it will fail when we it check it with "is" for Array type ( is Array will return false ). More infomration about using a proxy class can be found on my website.
This function wil create an array with size - nSize and initialize all elements.
function fnc( nSize: int, type:* ): Array
{
// Creating an array with requested size - nSize.
// Creating an array with size (new Array( nSize )) is a faster way
// to create an array instead of using push method in loop nSize-times .
var arr: Array = new Array( nSize );
// This is a faster way - one if and one loop itteraction
if( type is Class )
{
// if the passed type is a class. The initializing is creation of an instance of this class for every element of the array
for ( var iPos: uint = 0; iPos < nSize; ++iPos )
{
arr[ iPos ] = new type();
}
}
else
{
//type is value so assign this value on every element of the array
for ( var iPos: uint = 0; iPos < nSize; ++iPos )
{
arr[ iPos ] = type;
}
}
return arr;
}
Test cases:
Creating an Array with three elements initialized with value 7;
var arr: Array = fnc( 3, 7 );
trace( arr ); //output 7,7,7
class S
{
public var t: int =7;
}
function initializeArray( arrToInit: Array, type: * )
{
if ( type is Class )
{
arrToInit.forEach( initWithClass );
}
else
{
arrToInit.forEach( initWithValue );
}
function initWithClass(element:*, index:int, arr:Array):void
{
arr[ index ] = new type();
}
function initWithValue(element:*, index:int, arr:Array):void
{
arr[ index ] = type;
}
}
+