[SE-0415] Add documentation for body macros#443
[SE-0415] Add documentation for body macros#443literally-anything wants to merge 1 commit intoswiftlang:mainfrom
Conversation
Body macros are a stable language feature, but they are't mentioned anywhere except the evolution proposal.
amartini51
left a comment
There was a problem hiding this comment.
Thanks for taking the time to add this to the documentation! Sorry for a bit of delay last week, while I was working on some other documentation.
Reading this PR and SE-0415, I think there are two things we can check on. Tagging @DougGregor as the proposal author, for technical review — maybe Doug can answer these.
Body macros can go on property accessors, like get and set — not just functions and methods. Is that specific to body macros, or is it also true of all attached macros? Let's add it to the reference either here, or for attached macros in general.
The proposals notes that body of a function marked with this macro must be syntactically valid Swift code, although it doesn’t need to typecheck. The "Macros" chapter in TSPL says that normally code marked with macros does need valid type information:
Both the input to a macro and the output of macro expansion are checked to ensure they’re syntactically valid Swift code. Likewise, the values you pass to a macro and the values in code generated by a macro are checked to ensure they have the correct types.
If the "no type checking" rule is specific to body macros, let's add it here — otherwise, at the relevant spot in the reference.
| - term Body macros: | ||
| Write `body` as the first argument to this attribute. | ||
| The type that implements the macro conforms to the `BodyMacro` protocol. | ||
| These macros modify or replace the body of the function that they are attached to. | ||
| If the function has no body, | ||
| the macro can generate one from scratch. | ||
| Only one body macro is allowed on a function at a time. |
There was a problem hiding this comment.
Some wording adjustments:
| - term Body macros: | |
| Write `body` as the first argument to this attribute. | |
| The type that implements the macro conforms to the `BodyMacro` protocol. | |
| These macros modify or replace the body of the function that they are attached to. | |
| If the function has no body, | |
| the macro can generate one from scratch. | |
| Only one body macro is allowed on a function at a time. | |
| - term Body macros: | |
| Write `body` as the first argument to this attribute. | |
| The type that implements the macro conforms to the `BodyMacro` protocol. | |
| These macros can generate, modify, or replace | |
| the body of the function that they're attached to. | |
| You can write at most one body macro on any given function. |
I think it reads better to include the possibility of generating the function body as part of the sentence about replacing or modifying the body — that way, all three options are in one place.
|
CC @invalidname who will be working on Swift docs when I'm out of the office starting sometime next month. |
Body macros are a stable language feature, but they are't mentioned anywhere except the evolution proposal. This adds
bodyas a possible type of attached macro in the@attachedattribute section.Fixes: rdar://171280404