Created
June 11, 2023 13:35
-
-
Save Adobels/8e8e3fee460890014beeeb116a2f5b79 to your computer and use it in GitHub Desktop.
Swift-macros quick notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
created_at: Sun, 11 Jun 2023 13:31:06 GMT, Xcode 15.0.0-beta | |
Swift-macros quick notes | |
Related videos: | |
- [WWDC23 What’s new in Swift](https://developer.apple.com/videos/play/wwdc2023/10164) | |
- [WWDC23 Write Swift macros](https://developer.apple.com/videos/play/wwdc2023/10166) | |
- [WWDC23 Expand on Swift macros](https://developer.apple.com/videos/play/wwdc2023/10167) | |
It looks like: | |
- Swift-Macros can replace code generation tools like SwiftGen or work in a similar way to SwiftLint Analyzer or Periphery. | |
- Swift-Macros can be a tool for cases when protocols, generics, or OOP can't be a solution. | |
In the video "WWDC23 What’s new in Swift," Doug Gregor mentions, "The assert macro here comes from the power asserts library, an open-source Swift package available on GitHub." The repository is [link](https://github.com/DougGregor/swift-macro-examples). | |
When I created the first local package with a macros template and integrated it into a demo app, I encountered compilation errors stating that I couldn't import the package with macros. I tried creating the same package and pushing it to GitHub, integrating it with the demo app, but I still faced the same problem. This issue might be related to the fact that Swift Package Plugins need to be accepted within Xcode with the "Trust & Enable" action. Additionally, I found that when working with the demo app, I was unable to open the Swift macro package file; only the package file itself was accessible, but its content was unavailable. | |
During my work on custom macros, I discovered that macros are built over the SwiftSyntax project, and its API is not easy to work with without proper documentation. To overcome this, I had to find the documentation, which is accessible here: [Swift-Syntax documentation](https://swiftpackageindex.com/apple/swift-syntax/508.0.1/documentation/swiftsyntax). | |
Another useful tool I found was https://swift-ast-explorer.com, which helped me understand how the code is parsed into an AST tree, aiding my usage of the Swift-Syntax API to create returned code. | |
After several attempts to create custom macros, I encountered a situation where the demo app integrated multiple macro packages, some of which had macros with the same name. This resulted in compilation errors due to conflicting macros in different packages. I raised a question about differentiating macros from specific packages on Apple's developer forums. | |
Another consideration is that when integrating third-party macro packages, the project becomes dependent on the Swift-Syntax project as well. Building the Swift-Syntax project on a MacBook Air M1 took approximately 30 seconds. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment