This may be useful for someone, maybe. I don’t know. But I was bored so I made it. It’s an Array that only accepts one type of data. The first argument of the constructor is the data type that the array contains, the second is a filter boolean and the rest are values to be added to the array. The filter property determines whether to throw an error if another data type is added or to remove the value.
I’ll look into any errors/suggestions.
package {
dynamic public class TypedArray extends Array {
protected var pType:Class;
protected var pFilter:Boolean;
public function TypedArray(t:Class, f:Boolean = false, ... values) {
pType = t;
pFilter = f;
push.apply(this, values);
}
override AS3 function push(... values):uint {
return super.push.apply(this, values.filter(test));
}
override AS3 function splice(... values):* {
return super.splice.apply(this, values.slice(0, 2).concat(values.slice(2).filter(test)));
}
override AS3 function unshift(... values):uint {
return super.unshift.apply(this, values.filter(test));
}
public function get type():Class {
return pType;
}
public function get filter():Boolean {
return pFilter;
}
protected function test(item:*, ... values):Boolean {
if(item is pType) return true;
else if(filter) return false
else throw new TypeError("All values must be of type " + pType);
}
}
}
:hoser: