Why can't SwiftMacro communicate with each other?

Hello! Why they can not communicate with each other? Macro declared in another expanded macro is not visible for compiler and just ignored. Is it a bug or is a limit of macro system? Maybe it will be fixed in future?

What do you mean by "communicate"?

Regardless, macros can only work on the syntax they're provided or are attached to.

I mean if one macro generates by another macro then inner macro will not be expanded

It should do!

Xcode's "Expand macro" feature only expands macros by one level, I believe, because that's how the macro works. You should be able to expand the #Preview macro in that as well, and see what's happening.

You can expand it manually, but the inner #Preview won't make Xcode display the preview pane. I too would like to know if this is intended or just not implemented, as I'm facing the exact same situation (with inner #Preview).

(why? See How to get access to a View defined inside a #Preview macro)

Xcode does weird things with to make SwiftUI previews possible, especially with #Preview. For example, it generates an arbitrarily named macro at the global scope without declaring any names on the macro declaration and the new structure is hidden from user code - Xcode takes any preview-code and handles it separately, but only if it's written in an expected place within the raw file/AST.

However that's all from my own investigation of trying to reverse engineer how previews work, and I could be wrong.

1 Like