Macro or feature for dynamic field access

I have a lot of code taking many keys from a dynamic var qry.name qry.address etc, I want to change the dynamic to some type that would use the string to calculate the value, like es6 proxy, one way is with abstract arrayAccess but this will require me to change all calls to qry[“name”], can this be done in haxe?

var name=qry.name;
//should become
var name=qry.get("name");

It just so happens I have this bit of code obtained from @back2dos :

typedef SomeData = {
	final attributes:Attributes;
}

abstract Attributes(Dynamic<String>) from Dynamic<String> {
	@:op(a.b) public inline function get(name:String):String return untyped this[name];

	public function keyValueIterator()
		return (this:haxe.DynamicAccess<String>).keyValueIterator();
}

So the attributes would have a get method which operates as you describe, e.g.: data.attributes.get(name); (without the quotation marks). Remember the “name” in the get method is simply any key you are trying to retrieve.

There are many options. One of them would be to traverse the data (if it is traversible) and build a new object from it.

Another one is the one suggested by Allan.

It also depends on how you want to use the whole thing. Is type safety a concern?

1 Like