I declared the expression Macro and I have retrieved the parameters from FreestandingMacroExpansionSyntax as below.
Macro declaration
public macro sfImage<T>(_ value: String) -> T
Retrieve parameters
public struct sfImage: ExpressionMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> ExprSyntax {
guard let argument = node.argumentList.first?.expression else {
fatalError("compiler bug: the macro does not have any arguments")
}
// Return Image or UIImage based on return type.
return ""
}
}
I would like to get the return type of Macro by parsing FreestandingMacroExpansionSyntax. Please suggest how can we retrieve the return type of macro. Thanks in advance.
I believe there is no way to do this.
FreestandingMacroExpansionSyntax is the macro body, which does not specify the return type. Like, if I wrote this:
let image = #sfImage("foo_bar.png") as Image
The macro would receive this.
#sfImage("foo_bar.png")
You need to either define two separate macros
#sfUIImage("foo_bar.png")
#sfSwiftUIImage("foo_bar.png")
or put the type as a parameter.
#sfImage("foo_bar.png", as: Image.self)
1 Like
Thanks Aggie33 for the valuable Information.