I want to write a macro GetSetMacro
which is taking in the value of the stored property of the type and generate the accessors for another property based on it.
// Definition
@attached(accessor)
public macro GetSetMacro<Value>(_ : Value) = #externalMacro(module: "MyMacroModule", type: "GetSetMacro")
// Implementation
public struct GetSetMacro: AccessorMacro {
public static func expansion(of node: SwiftSyntax.AttributeSyntax, providingAccessorsOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.AccessorDeclSyntax] {
guard
case let .argumentList(arguments) = node.arguments,
let argument = arguments.first
else { return [] }
return [
"""
get {
// How to access the member value of the type in here?
???
}
""",
"""
set {
??? = newValue
}
"""
]
}
}
Example usage
var value = 1
@GetSetMacro("value")
var newIntValue: Int
// expansion
var newIntValue {
get {
value
} set {
value = newValue
}
}