I'm attempting to generate default implementations for a protocol using a macro. The goal is something like this:
protocol Foo {
var one: String { get }
func two()
}
@GenerateDefaultImplementations
extension Foo {} // add generated defaults for `var one: String` and `func two()`.
I have GenerateDefaultImplementations
set up as a MemberMacro
however I'm struggling to figure out how I can access the members of Foo
from the macro that is on the extension.
struct GenerateDefaultImplementations: MemberMacro {
static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard let ext = declaration.as(ExtensionDeclSyntax.self) else {
return []
}
guard let extendedType = ext.extendedType.as(SimpleTypeIdentifierSyntax.self) else {
return []
}
// âť“ How can I access the functions of `extendedType` (in this case `Foo`) here?
}
}
Is it possible to access the functions of Foo
in a macro on this extension, even though it was defined elsewhere? If so, how can I do that?