# Macro or feature for dynamic field access

**URL:** <https://community.haxe.org/t/macro-or-feature-for-dynamic-field-access/4312>\
**Category:** Haxe\
**Created:** [August 19, 2024, 1:36pm UTC](https://community.haxe.org/t/macro-or-feature-for-dynamic-field-access/4312 "2024-08-19T13:36:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![neimanpinchas](https://community.haxe.org/user_avatar/community.haxe.org/neimanpinchas/32/2348_2.png) [@neimanpinchas](https://community.haxe.org/u/neimanpinchas)\
**Post date:** [August 19, 2024, 1:36pm UTC](https://community.haxe.org/t/macro-or-feature-for-dynamic-field-access/4312/1 "2024-08-19T13:36:22Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![Confidant](https://community.haxe.org/user_avatar/community.haxe.org/confidant/32/63_2.png) [@Confidant](https://community.haxe.org/u/Confidant)\
**Post date:** [August 20, 2024, 2:56pm UTC](https://community.haxe.org/t/macro-or-feature-for-dynamic-field-access/4312/2 "2024-08-20T14:56:00Z")

</div>

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

```haxe
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.

---

<div class="post-metadata">

**Author:** ![back2dos](https://community.haxe.org/user_avatar/community.haxe.org/back2dos/32/30_2.png) [@back2dos](https://community.haxe.org/u/back2dos)\
**Post date:** [August 21, 2024, 9:36am UTC](https://community.haxe.org/t/macro-or-feature-for-dynamic-field-access/4312/3 "2024-08-21T09:36:27Z")

</div>

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?
