Dynamic Data Typing

Here is something for you to play around with…

I will give you a little idea… and see what you guys can do to come up with stuff.

The only reason I’m not going to go into detail with this is, I gotta keep some of us programmers job security safe ;). This is merely to inspire.


var myClass:Function = Array;

var myArray:Object = new myClass();

trace( myArray instanceof myClass);

myArray.push(5);
myArray.push(10);

trace( myArray );

I may come back with a more elaborate example… but I may not… enjoy

TakeCare…
_Michael

of course this is coming from the guy who lost his job this week…

lol… bastard.

lol… well here’s one example… that I’ll TELL you…

I have a collection that is similar to a generic in Java… or similar to arrays in java with the lack of implementation

It’s a collection that can be typed, will only hold the Type you specify.

Happy Programming :slight_smile:

_Michale

something that I’ve done in Modula-2 doesn’t inspire me doing with AS
maybe when I need it I will
so what’s next :wink:

Here is the first example…

GenericCollection.as


/**
 * An example file for our generic collections class..
 *
 * @author Michael Avila
 * ...
 * @version 1.0.0
 */
class GenericCollection
{

	// the datatype that this collection holds
	private var thetype:Function;

	// the collection interior
	private var collection:Array;

	/**
	 * Constructor
	 *
	 * @param The datatype this collection will hold
	 */
	public function GenericCollection( datatype:Function )
	{

		if (datatype == undefined)
		{
			trace("Constructor GenericCollection requires parameter datatype");
		}
		thetype = datatype;
		collection = new Array();
	}

	/**
	 * Adds an item to the collection... must be of the specified type
	 */
	public function add( object:Object ):Boolean
	{
		if ( !(object instanceof thetype) )
		{
			trace("Invalid type: Class GenericCollection");
			return false;
		}
	   	var len:Number = collection.length;
	   	for (var i:Number = 0; i < len; i++)
		{
			if (collection* == object) return false;
		}

		collection.push( object );
		return true;
	}

	/**
	 * Returns the object at the specified index.
	 */
	public function get( index:Number )
	{
		return thetype( collection[index] );
	}

	/**
	 * Removes the item from the collection
	 */
	public function remove( object:Object ):Object
	{
		var len:Number = collection.length;
		for (var i:Number = 0; i<len; i++)
		{
			if ( collection* == object ) return collection.splice(0, 1);
		}
	}

	/**
	 * Removes the item at the specified index
	 */
	public function removeAt( index:Number ):Object
	{
		return collection.splice(index, 1);
	}
}

Here is it’s usage so far in the fla


var gcol = new GenericCollection( Array );

trace( gcol.add( new Array(5, 10, 15) ) );
trace( gcol.add( new Object() ) );

trace( gcol.removeAt(0) );

Feel free to ask anyquestions.

TakeCare
_Michael

The only thing I’m having a hard time overcoming is that this will not work with primitive data types (Number, String, Boolean).

TakeCare
_Michael

number, string and boolean are abstract datatypes not primative

Where exactly did you hear that?

The specification states that the following are primitive data types:

Boolean
null
Number
String
undefined

I am not saying that you are wrong, I would just like to know.

TakeCare
_Michael

The spec may state that they are, but thats quite wrong - because they have methods provided that allow you to operate on the data contained, this makes them abstract.

From the language def…

The String class is a wrapper for the string primitive data type, and provides methods and properties that let you manipulate primitive string value types.

The Number class is a simple wrapper object for the Number data type - number cant be a primitive, how can a primitive handle, int, bigint, double, single and so on…

The Boolean class is a wrapper object with the same functionality as the standard JavaScript Boolean object.

and so on…

I’m just being pedantic and you probably meant string rather than String, I should probably be ignored lol, but a primitive is completely different, it is a base type that provides a container for data with a certain amount of bytes allocated in the stack when its created, the abstraction happens when you add functionality to this base type, ala String and so on…