views:

37

answers:

1

Because of some limitations (example) in the built-in ActionScript 3 Dictionary class I'm looking to build a wrapper which adds such things. Is it possible to keep the syntax below for my custom class, and if so how?

var dic:MyDic = new MyDic();
dic[stuffy] = someObject;
+2  A: 

Yes you can using Proxy function but with performance overhead:

import flash.utils.Dictionary;
import flash.utils.Proxy;
import flash.utils.flash_proxy;

public class MyDict extends Proxy {
    private var _size:int = 0;
    private var _dict:Dictionary = new Dictionary();

    public function get size():int {
        return _size;
    }

    flash_proxy override function getProperty(name:*):* {
        return _dict[name];
    }

    flash_proxy override function setProperty(name:*, value:*):void {
        if (!_dict.hasOwnProperty(name))
            _size ++;
        _dict[name] = value;
    }

    flash_proxy override function deleteProperty(name:*):Boolean {
        if (_dict.hasOwnProperty(name)) {
            _size --;
            delete _dict[name];
            return true;
        }
        return false;
    }
}

var dict:MyDict = new MyDict();
dict[1] = 2;
dict["foo"] = "bar";
trace(dict.size, dict[1], dict["foo"]);

delete dict[1];
trace(dict.size, dict[1], dict["foo"]);
Patrick
Thanks, but the performance overhead is not worth it. I'll make do with add() and remove() functions :)
Bart van Heukelom