Member-only story
WWDC 2023: Write Swift Macros
This one is exciting
10 min readJun 21, 2023
This article is intended as a companion to Apple’s video and does not replace it in any way. However, I find having a text file like this much more helpful than just a video but I’m sure you’ll let me know in the comments how useful this is.
Macros allow you generate repetitive code at compile time. This is what is shown in the video and this article.
Create a macro
File> New> Package
Then select the Swift Macro template
Which, as in the video I shall call “WWDC”
In the WWDC client folder we get the following code:
import WWDC
let a = 17
let b = 25
let (result, code) = #stringify(a + b)
print("The value \(result) was produced by the code \"\(code)\"")