Skip to content

Commit 26ff98a

Browse files
Update nnnn-reusable-unique-names-in-macros.md
1 parent 16b54bf commit 26ff98a

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

proposals/nnnn-reusable-unique-names-in-macros.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ As outlined above, these mangled unique names are specific to the role of the ma
6868
@attached(accessor)
6969
macro AssociatedObject() = #externalMacro(...)
7070

71-
// Macro usage
71+
// Macro usage (located in module "MyModule")
7272
class MyClass {
7373
@AssociatedObject
7474
var foobar: AnyObject?
@@ -86,6 +86,21 @@ class MyClass {
8686
}
8787
```
8888

89+
The implementation of this macro will conform to the `PeerMacro` and the `AccessMacro` protocols to fulfil the two macro roles that are declared on the macro. The implementation of the `PeerMacro` is responsible for creating the declaration of the static variable for the associated object key. In doing this it needs to create a unique identifier. The identifier might look something like the following:
90+
91+
```swift
92+
extension AssociatedObjectMacro: PeerMacro {
93+
static func expansion(
94+
of node: AttributeSyntax,
95+
providingPeersOf declaration: some DeclSyntaxProtocol,
96+
in context: some MacroExpansionContext
97+
) throws -> [DeclSyntax] {
98+
let uniqueVariableName = context.makeUniqueName("associatedObjectKey")
99+
// uniqueVariableName == "$s8MyModule7MyClass6foobarfMp_19associatedObjectKeyfMu_"
100+
}
101+
}
102+
```
103+
89104
## Proposed solution
90105

91106
Describe your solution to the problem. Provide examples and describe

0 commit comments

Comments
 (0)