Jeehut
(Cihat Gündüz)
1
I just had an idea for an architectural-level macro where my macro would help structure features of an app. I have not had the time to play around with the idea yet (to figure out if a Macro is even a good choice), but some of the "expanded" code of my Macro could potentially involve other macros, such as @Observable. I wonder if macro expansion works even with nested macros?
I haven't found anything on the topic in this forum or in the Macros Vison document (I searched for "nested" and "recursive" macros). Would be really useful if nested/recursive macros were supported. 
mlienert
(Martial Lienert)
2
It's working.
As an example the Model macro from SwiftData expansion contains others macros like PersistedProperty macro.
Xcode also supports expanding nested macro in the IDE.
7 Likes
dmt
(Dima Galimzianov)
4
I think it's worth to mention that recursive macros are not supported. I asked about this at WWDC and received the following response:
The compiler will emit an error if there is a cycle in the expansion of macros.
hborla
(Holly Borla)
5
Nested macro expansion is supported. You will definitely be interested in member-attribute macros which primarily exist to facilitate nested macro expansion like you're describing. This is what @Observable uses to apply accessor macros to all properties within a type.
1 Like
avanishr
(Avanishr)
6
Are there any examples of this in action for reference?
For example, the @Model macro from SwiftData, among other things, adds the @PersistedProperty macro to all stored properties.
2 Likes
Is it possible for one macro to nest more macros on the declaration itself, for example, would it be possible to have something like this:
@MacroA
enum Fruit {
case apple, banana
}
On expanding MacroA:
@MacroB
@MacroC
enum Fruit {
case apple, banana
}