# Macro for accessing deeply nested properties

**URL:** https://community.haxe.org/t/macro-for-accessing-deeply-nested-properties/2155
**Category:** Haxe
**Tags:** macro
**Created:** [November 24, 2019, 12:22pm UTC](https://community.haxe.org/t/macro-for-accessing-deeply-nested-properties/2155 "2019-11-24T12:22:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![fed239](https://community.haxe.org/user_avatar/community.haxe.org/fed239/32/941_2.png) [@fed239](https://community.haxe.org/u/fed239)
#### Post date: [November 24, 2019, 12:22pm UTC](https://community.haxe.org/t/macro-for-accessing-deeply-nested-properties/2155/1 "2019-11-24T12:22:31Z")

</div>

Hi all!

I’d like to use a macro, which would allow access deeply nested properties or return default value or raise an exception containing where access has failed.

We implemented similar functionality for `Dynamic` data structures using reflect, but obviously it lacks static type checking and existence of the fields can’t be checked during compilation time.

My colleague tried to implement this macro and got some basics done, but since we don’t have enough experience with Haxe macro (and probably even full understanding) we are not sure what would be the best approach for this.

Also I may assume that this functionality is already available in some library.

Could anyone suggest how to address this or maybe suggest a library, if this is implemented already?

Big thanks in advance!

---

<div class="post-metadata">

### Author: ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)
#### Post date: [November 24, 2019, 12:31pm UTC](https://community.haxe.org/t/macro-for-accessing-deeply-nested-properties/2155/2 "2019-11-24T12:31:03Z")

</div>

Safety library has similar feature: [GitHub - RealyUniqueName/Safety: Null safety for Haxe](https://github.com/RealyUniqueName/Safety#safe-navigation-operator).  
For `obj!.field` expression it returns `obj.field` value or `null` if `obj` is `null`.  
Here is the code, which transforms `obj!.field`: [Safety/SafeAst.hx at master · RealyUniqueName/Safety · GitHub](https://github.com/RealyUniqueName/Safety/blob/master/src/safety/macro/SafeAst.hx#L192-L198)  
Maybe that could give you a hint

---

<div class="post-metadata">

### Author: ![fed239](https://community.haxe.org/user_avatar/community.haxe.org/fed239/32/941_2.png) [@fed239](https://community.haxe.org/u/fed239)
#### Post date: [December 13, 2019, 8:20am UTC](https://community.haxe.org/t/macro-for-accessing-deeply-nested-properties/2155/3 "2019-12-13T08:20:49Z")

</div>

Your library looks like what we need. So I believe we shouldn’t reinvent this, but rather use your code. Because you are surely way way more advanced.

One question, related to this. Let’s say we are accessing some deeply nested field like in the following example and `nestedField` is null.

```haxe
var v:String = obj!.field!.nestedField!.value;

```

Would we be able to see from the exception raised that it was the `nestedField` belonging to `obj.field`?

We’d like to see something like “Unable to access value of field.nestedField, because it is null”. If this sort of “context aware” exceptions is not supported in the Safety library, could you suggest a strategy for implementing such behaviour?

---

<div class="post-metadata">

### Author: ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)
#### Post date: [December 13, 2019, 8:24am UTC](https://community.haxe.org/t/macro-for-accessing-deeply-nested-properties/2155/4 "2019-12-13T08:24:50Z")

</div>

There will be no exception, because `!.` in my lib is like `?.` in other languages. Haxe doesn’t have postfix `?` operator, so I had to take `!`.  
In your example `v` will be `null` if `obj!.field!.nestedField` is `null`.

---

<div class="post-metadata">

### Author: ![fed239](https://community.haxe.org/user_avatar/community.haxe.org/fed239/32/941_2.png) [@fed239](https://community.haxe.org/u/fed239)
#### Post date: [December 13, 2019, 9:51am UTC](https://community.haxe.org/t/macro-for-accessing-deeply-nested-properties/2155/5 "2019-12-13T09:51:44Z")

</div>

Ah, true. That was a dumb question 🙂 because I’m mixing in my ahead a few related features we need. Basically when accessing unavailable deeply nested field we depending of the situation would need to either:

- get null (!. syntax is very nice)
- get default value (i saw `Safety` has `.or` macro?)
- throw an exception explaining where the problem occurred (i also saw `.sure` and that made me extremely excited)

We just need to play around with your library to get real understanding. So thanks a lot and I’ll get back when we know more.

---

<div class="post-metadata">

### Author: ![pbotsman](https://community.haxe.org/user_avatar/community.haxe.org/pbotsman/32/1103_2.png) [@pbotsman](https://community.haxe.org/u/pbotsman)
#### Post date: [December 16, 2019, 3:00pm UTC](https://community.haxe.org/t/macro-for-accessing-deeply-nested-properties/2155/6 "2019-12-16T15:00:36Z")

</div>

Hi everyone.

I am from Fedor’s team 🙂

I am doing some experiments with nullability and facing some unclear for me behavior.

I have the following lines in my `.hxml`:

```haxe
-lib safety
--macro Safety.safeNavigation("project", true)
--macro nullSafety("project", Strict)

```

And here is my code:

```haxe
var data: Dynamic = getDynamicData();
var value: Null<String> = data!.a!.b!.c!.d!.f;
value.sure(); // works fine

data!.a!.b!.c!.d!.f.sure(); // compile error. Null safety: Cannot access "sure" of a nullable value.

```

As far as I understand, in both cases where `.sure()` is called, the type is `Null<T>`.  
Could you tell me please, why I am facing such error?  
Do I understand something wrong or maybe there is a some bug?

Thanks!

---

<div class="post-metadata">

### Author: ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)
#### Post date: [December 17, 2019, 11:22am UTC](https://community.haxe.org/t/macro-for-accessing-deeply-nested-properties/2155/7 "2019-12-17T11:22:13Z")

</div>

It’s because the type of `data!.a!.b!.c!.d!.f` is `Dynamic` in which case Haxe compiler does not call `Safety.sure()`, but assumes that the dynamic value has it’s own field `sure` and generates a field access on the value instead of a call to the static extension.
