# Use a macro to modify nested structs?

**URL:** <https://forums.swift.org/t/use-a-macro-to-modify-nested-structs/78586>\
**Category:** Macros\
**Created:** [March 17, 2025, 8:33pm UTC](https://forums.swift.org/t/use-a-macro-to-modify-nested-structs/78586 "2025-03-17T20:33:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MisterDrgn](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@MisterDrgn](https://forums.swift.org/u/MisterDrgn)\
**Post date:** [March 17, 2025, 8:33pm UTC](https://forums.swift.org/t/use-a-macro-to-modify-nested-structs/78586/1 "2025-03-17T20:33:44Z")

</div>

Suppose I have a setup like this (I'm simplifying things a lot).

```swift
@Domain
struct SimpleDomain {
    struct Properties {
        var color: Color
        var area: Int
    }
    
    struct ObjectProperties {
        var object: Object
    }
    
    struct GroupProperties {
        var group: Group
    }
}

```

Now suppose I want the Domain macro to find the struct named `Properties`, extract its vars, and insert them into the other structs as computed properties. Is there any way to do this?

I'm experimenting with Domain as a member macro. I can find each of the nested structs, and find all of their vars. But I don't see any way I can modify `ObjectProperties` and `GroupProperties`. I don't think there's any way for a macro working at the level of the `SimpleDomain` struct to insert code inside the nested structs. And I can't extend `ObjectProperties` because you can't extend a struct inside another one (I know you can use `extension SimpleDomain.ObjectProperties`, but I doubt any macro can do that).

Alternatively, I could try applying a member macro directly to `ObjectProperties` and `GroupProperties`. But I don't know how that macro would have access to the vars defined inside `Properties`.

Any suggestions would be welcome. Thanks.

---

<div class="post-metadata">

**Author:** ![jlukas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jlukas/32/317_2.png) [@jlukas](https://forums.swift.org/u/jlukas)\
**Post date:** [March 18, 2025, 1:02pm UTC](https://forums.swift.org/t/use-a-macro-to-modify-nested-structs/78586/2 "2025-03-18T13:02:21Z")

</div>

I’ve done something similar. What you need to do is use the outer macro to apply a macro attribute to the structs you want to modify, including all of the information from Properties you need as arguments to the macro.

---

<div class="post-metadata">

**Author:** ![MisterDrgn](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@MisterDrgn](https://forums.swift.org/u/MisterDrgn)\
**Post date:** [March 18, 2025, 2:08pm UTC](https://forums.swift.org/t/use-a-macro-to-modify-nested-structs/78586/3 "2025-03-18T14:08:54Z")

</div>

> [@jlukas](#):
>
> do is use the outer macro to apply a macro attribute to the structs you want to modify, including

Thanks, this sounds like the right approach. I take it this means using MemberAttributeMacro? I haven't used that one, but I found an example here (I always struggle to find good official documentation on macros):

> **[Swift Macros: Detailed & Practical Understanding](https://medium.com/@canakyildz/swift-macros-detailed-practical-understanding-829d88d337fb)**
>
> Hey guys, today I will be talking about Swift Macros.

So it looks like this can take a member, check whether it's one of the ones I want to modify, and if it is, add a nested macro which will take the properties as arguments. And I can use the attachedTo argument to get my outer struct, so I can find the properties to apply to the appropriate inner structs?

---

<div class="post-metadata">

**Author:** ![jlukas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jlukas/32/317_2.png) [@jlukas](https://forums.swift.org/u/jlukas)\
**Post date:** [March 18, 2025, 2:14pm UTC](https://forums.swift.org/t/use-a-macro-to-modify-nested-structs/78586/4 "2025-03-18T14:14:29Z")

</div>

It’s been awhile since I implemented it, but yes, that sounds correct.
