# \[Pitch\] Declaration macros

**URL:** <https://forums.swift.org/t/pitch-declaration-macros/62373>\
**Category:** Pitches\
**Created:** [January 4, 2023, 12:28am UTC](https://forums.swift.org/t/pitch-declaration-macros/62373 "2023-01-04T00:28:53Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![s-k](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/s-k/32/6796_2.png) [@s-k](https://forums.swift.org/u/s-k)\
**Post date:** [January 6, 2023, 7:11am UTC](https://forums.swift.org/t/pitch-declaration-macros/62373/19 "2023-01-06T07:11:54Z")

</div>

> [@Douglas\_Gregor](#):
>
> we should separate out the ability to add or replace a function body from the ability to add peer declarations, attributes, etc.

While I fear that this may delay the ability to work with functions, I think this is a good decision if we get a nicer API this way.

> [@allevato](#):
>
> As a general feature, I really like where this is going, and I think that that there are certain examples (like those highlighted in the proposal) where purely syntactic introspection can be powerful enough for what users need, but I wonder how many times users would hit a wall where we need some semantic data. Do you have any more insight here?

I feel that it could be valuable to have a general idea how type information would be supplied to the macro. It may have consequences for the design of the basic feature (even if type information is added in a future proposal). While thinking about and implementing different possible macros, I have repeatedly seen the need to have type info. I think, there are (at least) two distinct needs:

1. Get the type of a (sub-)expression supplied to the macro. This can especially be useful for expression macros, but also for macros modifying function bodies. And since the compiler has already type-checked the code, this info should be readily available. A macro implementing something like function builders would definitely need this. It may be possible to somehow do some hacks involving type inference to make it work, but it wouldn't be nice. ([SE-0380 (`if` and `switch` expressions)](https://forums.swift.org/t/se-0380-if-and-switch-expressions/61899) could help, but I am not sure.)

2. Get infos about any given type or protocol (conformances, protocol requirements, members, etc.). I see this as generally valuable for all sorts of macros. This would be some sort of reflection API. It would be very nice if it could mirror the [runtime reflection API](https://forums.swift.org/t/pitch-reflection/61438) as mentioned already [in that thread](https://forums.swift.org/t/pitch-reflection/61438/38). However, the currently pitched reflection API would not be enough because it does not supply conformances, computed properties and methods.

---

_[View the full topic](https://forums.swift.org/t/pitch-declaration-macros/62373)._
