In Objective-C we could write something like #define A B
and replace all "B" occurrences with "A" independently on their meaning.
Now in Swift 5.9 Apple introduced Swift Macro (which adds advanced tools to work with macros) but there is too little information about it and I found how to use this technology with plain strings only:
@freestanding(expression)
public macro stringify() -> String = #externalMacro(module: "SomeMacroMacros", type: "StringifyMacro")
public struct StringifyMacro: ExpressionMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> ExprSyntax {
"\"Test\""
}
print(#stringify) //outputs "Test"
But what to do if I want to replace class, struct, protocol names declarations? Is it possible in general with Swift Macro?
Example
The following code doesn't work. I just use it to explain what I need to achieve with Swift Macro:
#define SomeClass SomeAnotherClass
class SomeClass {
...
}