Skip to content

create new abstract inside its method #3636

Closed
@ktimothy

Description

@ktimothy

Look at that code, especially the set method in DynamicArrayAccess

// need an array access on anonymous objects
abstract DynamicArrayAccess<T>(Dynamic<T>) from Dynamic<T>
{
    public inline function new(d:Dynamic<T>) this = d;

    @:arrayAccess
    public function set(key:String, value:T):Void
    {
        // Reflect.setProperty was the first decision
        // but I wanted to do it without Reflect
        // and I suddenly remembered, that my abstract is perfect for that
        new DynamicArrayAccess(this)[key] = value;
    }

    @:arrayAccess
    public inline function get(key:String):T
    {
        return Reflect.getProperty(this, key);
    }
}

class Test {
    static function main() {
        trace("Haxe is great!");

        var a:Dynamic<Int> = {};

        var a:DynamicArrayAccess<Int> = { k: 10 };
        a["k"] = 20;

        trace(a["k"]); // 20
    }
}

The generated in js setter look like

_Test.DynamicArrayAccess_Impl_.set = function(this1,key,value) {
    this1[key] = value;
};

How could that successfully compile? =)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions