# A Possible Vision for Macros in Swift

**URL:** <https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900>\
**Category:** Pitches\
**Created:** [October 17, 2022, 7:00pm UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900 "2022-10-17T19:00:12Z")\
**Posts on this page:** 17\
**Page:** 7

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [February 15, 2023, 11:33pm UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/123 "2023-02-15T23:33:32Z")

</div>

> [@stackotter](#):
>
> > Is it possible to add attributes through the macro (@dynamicMemberLookup)?
> 
> If macros are implemented as procedural code generators (like procedural macros in rust) then this would definitely be possible. I can’t really think of an approach where it wouldn’t be possible unless it was overlooked

It has neither been overlooked nor is it necessarily possible 🙂

The [attached macros proposal](https://github.com/apple/swift-evolution/pull/1932) provides more detail here, but we are not proposing to allow the kinds of completely arbitrary syntax rewriting that Rust's procedural macros do. Rather, we are proposing a more fine-grained approach that allows macros to make specific changes in an area that are tightly scoped and additive. This is better for compile-time performance, because the compiler doesn't need to expand all macros all the time to understand what the code does. I also feel that it's better for code clarity, because the changes a macro can make are mostly documented up-front, and have a more limited scope.

As for attributes specifically, it's something we can consider, although it's not in any proposal right now. We would need to make sure we can maintain the properties described above if we add the ability for a macro that's written as an attribute to in turn produce more attributes.

Doug

---

<div class="post-metadata">

**Author:** ![miku1958](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miku1958/32/11936_2.png) [@miku1958](https://forums.swift.org/u/miku1958)\
**Post date:** [February 16, 2023, 2:10am UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/124 "2023-02-16T02:10:09Z")

</div>

Vote for AttributeMacro, the potential of MemberMacro is huge, it provides a way to adjust members on a large scale, then AttributeMacro is necessary for small-scale filtering of certain members

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [February 16, 2023, 10:45pm UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/125 "2023-02-16T22:45:39Z")

</div>

> [@miku1958](#):
>
> Vote for AttributeMacro, the potential of MemberMacro is huge, it provides a way to adjust members on a large scale, then AttributeMacro is necessary for small-scale filtering of certain members

It occurs to me that we might be talking about "attribute macro" as meaning two different things:

1. A way to have an attached macro that generates other attributes.
2. A way to have an attached macro that _does nothing_, but whose presence can be used.

#1 is the one I'm concerned about from a semantic modeling perspective, although I see the utility.

#2 could be modeled by a peer macro that always expands to nothing.

Which of these are you thinking of, or is it both?

Doug

---

<div class="post-metadata">

**Author:** ![miku1958](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miku1958/32/11936_2.png) [@miku1958](https://forums.swift.org/u/miku1958)\
**Post date:** [February 17, 2023, 2:05am UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/126 "2023-02-17T02:05:45Z")

</div>

> [@Douglas\_Gregor](#):
>
> It occurs to me that we might be talking about "attribute macro" as meaning two different things:
> 
> 1. A way to have an attached macro that generates other attributes.
> 2. A way to have an attached macro that _does nothing_, but whose presence can be used.
> 
> #1 is the one I'm concerned about from a semantic modeling perspective, although I see the utility.
> 
> #2 could be modeled by a peer macro that always expands to nothing.
> 
> Which of these are you thinking of, or is it both?
> 
> Doug

The first one, it is similar to the MemberAttributeMacro, also return [SwiftSyntax.AttributeSyntax], but only works on the marked variable.

---

<div class="post-metadata">

**Author:** ![miku1958](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miku1958/32/11936_2.png) [@miku1958](https://forums.swift.org/u/miku1958)\
**Post date:** [February 17, 2023, 2:11am UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/127 "2023-02-17T02:11:32Z")

</div>

Another problem, I see @freestanding(declarations) used here for Codable, but I don't see how DeclarationMacro reads SwiftSyntax.DeclGroupSyntax like MemberMacro does

> <https://github.com/DougGregor/swift-evolution/blob/freestanding-macros/proposals/nnnn-freestanding-macros.md#up-front-declarations-of-newly-introduced-names>

So @freestanding(declarations) is just used here to give names as an example, right?

---

<div class="post-metadata">

**Author:** ![morpetlab](https://avatars.discourse-cdn.com/v4/letter/m/858c86/32.png) [@morpetlab](https://forums.swift.org/u/morpetlab)\
**Post date:** [February 17, 2023, 12:13pm UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/128 "2023-02-17T12:13:05Z")

</div>

Great work 😄 an easy use case could be to obfuscate/deobfuscate constants like API keys and such… looking forward to this 👌

---

<div class="post-metadata">

**Author:** ![stackotter](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stackotter/32/20670_2.png) [@stackotter](https://forums.swift.org/u/stackotter)\
**Post date:** [February 18, 2023, 3:48am UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/129 "2023-02-18T03:48:37Z")

</div>

> [@morpetlab](#):
>
> an easy use case could be to obfuscate/deobfuscate constants like API keys and such

It should be noted that if an API key is included in your app, obfuscation of it will make no difference and is security through obscurity. Only include an API key in your app if it is a public key such as a client/app id (E.g. the firebase key that you might include in your app). Any other keys should be hidden away on a backend server that your app interacts with through your own API.

If an API key is in your app (obfuscated or not) it can be obtained by anyone who wants to find it.

---

<div class="post-metadata">

**Author:** ![nuno-vieira](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nuno-vieira/32/27082_2.png) [@nuno-vieira](https://forums.swift.org/u/nuno-vieira)\
**Post date:** [June 8, 2023, 3:00am UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/131 "2023-06-08T03:00:23Z")

</div>

This would also be helpful for mocking 🤔, Example:

**Usage:**

```swift
@Mock
override func someMethod() {
}

```

**Result:**

```swift
var someMethodCallCount = 0
override func someMethod() {
    someMethodCallCount += 1
}

```

Is replacing declarations something the team is still considering?

Best,  
Nuno

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 8, 2023, 7:46am UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/132 "2023-06-08T07:46:01Z")

</div>

Hi, Nuno. You should check out [SE-0389](https://forums.swift.org/t/se-0389-attached-macros/63165) for more up-to-date information about that.

---

<div class="post-metadata">

**Author:** ![nuno-vieira](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nuno-vieira/32/27082_2.png) [@nuno-vieira](https://forums.swift.org/u/nuno-vieira)\
**Post date:** [June 9, 2023, 12:43am UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/133 "2023-06-09T00:43:06Z")

</div>

Thank you, John! 🙏

---

<div class="post-metadata">

**Author:** ![nuno-vieira](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nuno-vieira/32/27082_2.png) [@nuno-vieira](https://forums.swift.org/u/nuno-vieira)\
**Post date:** [June 9, 2023, 1:36am UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/134 "2023-06-09T01:36:31Z")

</div>

I just read it, but it doesn't mention anything about replacing declarations. It only talks about Attached Macros, which only add additional code, they don't replace existing declarations. 🤔

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [June 9, 2023, 1:39am UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/135 "2023-06-09T01:39:40Z")

</div>

You can absolutely think of your macro there as adding code rather than replacing it.

---

<div class="post-metadata">

**Author:** ![nuno-vieira](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nuno-vieira/32/27082_2.png) [@nuno-vieira](https://forums.swift.org/u/nuno-vieira)\
**Post date:** [June 9, 2023, 1:44am UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/136 "2023-06-09T01:44:47Z")

</div>

The problem is that for Mocks, adding code is not enough since the goal of a Mock is to replace the original implementation with a dummy one. A Peer Macro could solve this, but it would only work if the Mock is generated in the production code, which is not ideal. Unless there was a way to run a Macro only while Testing 🤔

---

<div class="post-metadata">

**Author:** ![Alejandro\_Martinez](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro_martinez/32/821_2.png) [@Alejandro\_Martinez](https://forums.swift.org/u/Alejandro_Martinez)\
**Post date:** [June 9, 2023, 7:44pm UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/137 "2023-06-09T19:44:18Z")

</div>

Something I’ve been doing with Sourcery for a while is the to have my implementation class, the generate a protocol for it and then a mock for that protocol. That is only adding code not replacing so it should work with macros?

---

<div class="post-metadata">

**Author:** ![linlinyao1](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/linlinyao1/32/30400_2.png) [@linlinyao1](https://forums.swift.org/u/linlinyao1)\
**Post date:** [June 15, 2023, 1:16pm UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/138 "2023-06-15T13:16:24Z")

</div>

Hi, I'm wondering when adding custom Swift Macro as a Swift Package to my project, will this macro library be embed into my app?

---

<div class="post-metadata">

**Author:** ![Zollerboy1](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zollerboy1/32/12997_2.png) [@Zollerboy1](https://forums.swift.org/u/Zollerboy1)\
**Post date:** [June 15, 2023, 2:38pm UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/139 "2023-06-15T14:38:42Z")

</div>

> [@linlinyao1](#):
>
> Hi, I'm wondering when adding custom Swift Macro as a Swift Package to my project, will this macro library be embed into my app?

No, the macro binary will be run as a compiler plugin while compiling, but will not be linked into other products (it can even be built for another target platform/architecture, e.g. when you're compiling on macOS for iOS).

---

<div class="post-metadata">

**Author:** ![nuno-vieira](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nuno-vieira/32/27082_2.png) [@nuno-vieira](https://forums.swift.org/u/nuno-vieira)\
**Post date:** [June 20, 2023, 1:51pm UTC](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900/140 "2023-06-20T13:51:42Z")

</div>

With Swift Macros, you can't access the protocol definition if the mock is in a different file. So the only way for that to work is to have the mock in the same file of the protocol/class you are trying to mock. Which is not good since the mocks will be shipped to the app, adding unnecessary size to the app.

[Previous page](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900.md?page=6)
