Sergey Senkov
Sergey Senkov

Reputation: 1540

AS3 TypedDictionary?

 public dynamic class TypedDictionary extends Dictionary {
    private var _type:Class;

    public function TypedDictionary(type:Class, weakKeys:Boolean = false) {
        _type = type;
        super(weakKeys);
    }

    public function addValue(key:Object, value:_type):void {
        this[key] = value;
    }

    public function getValue(key:Object):_type{
        return this[key];
    }
...

I want to make TypedDictionary with typisation. But I can not use _type in addValue and getValue. The idea is to use next construction :

var td:TypedDictionary = new TypedDictionary(myClass, true);
td.addValue("first", new myClass());
...
var item:myClass = td.getValue("first");
item.someFunction();

Is any ability to use dynamic class type?

Upvotes: 2

Views: 92

Answers (1)

The_asMan
The_asMan

Reputation: 6403

If I un derstand what you are asking for.
This is untested code so it could be error prone, but it should get you on the right path.

 public dynamic class TypedDictionary extends Dictionary {
    private var _type:Class;

    public function TypedDictionary(type:String, weakKeys:Boolean = false) {
        _type =  getDefinitionByName(type) as Class;
        super(weakKeys);
    }

    public function addValue(key:Object, value:*):void {
        if(_type == getDefinitionByName(getQualifiedClassName(value))){
            this[key] = value;
        }else{
            trace('failed type match')
        }
    }

    public function getValue(key:Object):*{
        return this[key];
    }
...


var td:TypedDictionary = new TypedDictionary("com.somepackage.myClass", true);
td.addValue("first", new myClass());

var item:myClass = td.getValue("first");
item.someFunction();

Upvotes: 1

Related Questions