# Getting started with macros?

**URL:** <https://forums.swift.org/t/getting-started-with-macros/62940>\
**Category:** Macros\
**Created:** [February 3, 2023, 6:35pm UTC](https://forums.swift.org/t/getting-started-with-macros/62940 "2023-02-03T18:35:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ron\_Olson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ron_olson/32/1325_2.png) [@Ron\_Olson](https://forums.swift.org/u/Ron_Olson)\
**Post date:** [February 3, 2023, 6:35pm UTC](https://forums.swift.org/t/getting-started-with-macros/62940/1 "2023-02-03T18:35:25Z")

</div>

Hi all. TIL Swift has macros per the newsletter I got. Are these C-style macros or something more Swift-y? Is there a page I can check out for getting started; my searches all come up with random SO pages that don’t seem a great way to get one’s feet wet with them.

---

<div class="post-metadata">

**Author:** ![Finagolfin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/finagolfin/32/34388_2.png) [@Finagolfin](https://forums.swift.org/u/Finagolfin)\
**Post date:** [February 3, 2023, 7:52pm UTC](https://forums.swift.org/t/getting-started-with-macros/62940/2 "2023-02-03T19:52:33Z")

</div>

Some [light](https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900) [reading](https://forums.swift.org/t/se-0382-expression-macros-mini-pitch-for-updates/62810) [for](https://forums.swift.org/t/pitch-attached-macros/62812) [you](https://forums.swift.org/t/pitch-freestanding-macros/62811). 😜

---

<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 3, 2023, 7:56pm UTC](https://forums.swift.org/t/getting-started-with-macros/62940/3 "2023-02-03T19:56:54Z")

</div>

Hello!

We're working on a macros feature for Swift. It's still experimental and going through a bunch of design evolution. Since it can be hard to track what's going on with the development of major features. I'm writing up a gist to help give an overview.

Here is a [Swift Macros Dashboard](https://gist.github.com/DougGregor/de840fcf6d6f307792121eee11c0da85).

Doug

---

<div class="post-metadata">

**Author:** ![mayoff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mayoff/32/613_2.png) [@mayoff](https://forums.swift.org/u/mayoff)\
**Post date:** [February 4, 2023, 12:15am UTC](https://forums.swift.org/t/getting-started-with-macros/62940/4 "2023-02-04T00:15:10Z")

</div>

I know it would be more work for you 😅 but I would love it if the dashboard had more info about what's actually implemented so far. For example, `swift-DEVELOPMENT-SNAPSHOT-2023-02-02-a.xctoolchain` (the latest atm) doesn't appear to support `@attached(peer)`.

---

<div class="post-metadata">

**Author:** ![hborla](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hborla/32/27563_2.png) [@hborla](https://forums.swift.org/u/hborla)\
**Post date:** [February 4, 2023, 12:37am UTC](https://forums.swift.org/t/getting-started-with-macros/62940/5 "2023-02-04T00:37:25Z")

</div>

I believe peer macros are the last major part of the attached macro proposal that is not implemented. I'm working on that, and I expect to have an initial implementation landed under the `Macros` experimental feature flag early next week!

---

<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 4, 2023, 9:58pm UTC](https://forums.swift.org/t/getting-started-with-macros/62940/6 "2023-02-04T21:58:11Z")

</div>

> [@mayoff](#):
>
> I know it would be more work for you 😅 but I would love it if the dashboard had more info about what's actually implemented so far. For example, `swift-DEVELOPMENT-SNAPSHOT-2023-02-02-a.xctoolchain` (the latest atm) doesn't appear to support `@attached(peer)`.

I added some details to the gist about what's expected to work (or not).

Doug

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [February 5, 2023, 8:54pm UTC](https://forums.swift.org/t/getting-started-with-macros/62940/7 "2023-02-05T20:54:05Z")

</div>

> [@hborla](#):
>
> I expect to have an initial implementation landed under the `Macros` experimental feature flag early next week!

is there a centralized guide for how to use the feature flags in SPM projects, and which ones are available for which nightly toolchain?

---

<div class="post-metadata">

**Author:** ![hborla](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hborla/32/27563_2.png) [@hborla](https://forums.swift.org/u/hborla)\
**Post date:** [February 6, 2023, 1:04am UTC](https://forums.swift.org/t/getting-started-with-macros/62940/8 "2023-02-06T01:04:34Z")

</div>

> [@taylorswift](#):
>
> is there a centralized guide for how to use the feature flags in SPM projects

[SE-0362](https://github.com/apple/swift-evolution/blob/main/proposals/0362-piecemeal-future-features.md) describes the `-enable-experimental-feature` mechanism and how to use it with SPM. You can also find the `SwiftSetting.enableExperimentalFeature` documentation in the SPM repository: [https://github.com/apple/swift-package-manager/blob/main/Sources/PackageDescription/BuildSettings.swift#L341](https://github.com/apple/swift-package-manager/blob/main/Sources/PackageDescription/BuildSettings.swift#L341)

> [@taylorswift](#):
>
> and which ones are available for which nightly toolchain?

I believe the only source of this information today is in the Swift repository for the tag of a given development snapshot in the Features.def file that declares these experimental features. For example, here's the Features.def file in the source code for the February 4th snapshot: [swift/include/swift/Basic/Features.def at swift-DEVELOPMENT-SNAPSHOT-2023-02-04-a · swiftlang/swift · GitHub](https://github.com/apple/swift/blob/swift-DEVELOPMENT-SNAPSHOT-2023-02-04-a/include/swift/Basic/Features.def). Everything declared with `EXPERIMENTAL_FEATURE` is available to use via `-enable-experimental-feature` in this snapshot.

---

<div class="post-metadata">

**Author:** ![filip-sakel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filip-sakel/32/8697_2.png) [@filip-sakel](https://forums.swift.org/u/filip-sakel)\
**Post date:** [February 6, 2023, 3:10am UTC](https://forums.swift.org/t/getting-started-with-macros/62940/9 "2023-02-06T03:10:27Z")

</div>

I think @Douglas_Gregor’s macro example repository also requires another flag (to dynamically load the plugins, IIRC?). Is that also required as an unsafe flag or can we just use the experimental feature flag for macros?

---

<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 6, 2023, 8:15pm UTC](https://forums.swift.org/t/getting-started-with-macros/62940/10 "2023-02-06T20:15:23Z")

</div>

> [@filip-sakel](#):
>
> I think @Douglas_Gregor’s macro example repository also requires another flag (to dynamically load the plugins, IIRC?). Is that also required as an unsafe flag or can we just use the experimental feature flag for macros?

Yeah, there are other flags to load the compiler "plugin". I wasn't able to express all of the right flags and dependencies in SwiftPM, so the [sample project](https://github.com/DougGregor/swift-macro-examples) is using Xcode's project model for the moment. It should be straightforward to implement the same set of dependencies in CMake, for example, but for SwiftPM we need an actual implementation of the manifest changes.

Doug
