-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vision for macros in Swift #1927
Conversation
Thank you @benrimmington Co-authored-by: Ben Rimmington <me@benrimmington.com>
Co-authored-by: Ben Rimmington <me@benrimmington.com>
Co-authored-by: Ben Rimmington <me@benrimmington.com>
General thoughts:
|
@beccadax the "attached macros" proposal has a lot more detail about macro roles and what it means to inhabit several different roles. Does that address your questions/concerns? Do you feel that the discussion needs to be lifted up into the vision document? I'm happy to workshop names like "codeItem" and "witness" a bit more. |
Maybe it's subtle enough that I overlooked it, but I don't quite see the answer to my question there; you mention that a macro can have multiple roles and that the compiler expands each of them, but I don't think you talk about what happens when only some of those roles are applicable. Let me make the question more concrete. Consider the @attached(peer, prefixed(_))
@attached(accessor)
macro Clamping<T: Comparable>(min: T, max: T) = #externalMacro(module: "MyMacros", type: "ClampingMacro") What happens if I do this? struct MyStruct {
@Clamping(min: 0, max: 255) func fn() { ... }
}
(
Probably not the whole discussion, but a brief mention of what it means when a macro has multiple roles might help readers understand how different roles are supposed to be used in concert. |
@beccadax we should move this discussion elsewhere. Attached macros has a pitch
I've added a paragraph to this effect in the attached macros proposal (7add644), but I think further discussion should occur on the forums, not here.
We have the |
visions/macros.md
Outdated
@@ -406,14 +440,14 @@ The `@freestanding` and `@attached` attributes for macro declarations specify th | |||
* **Body**: A body macro would allow one to create or replace the body of a function, initializer, or closure through syntactic manipulation. Body macros are attached to one of these entities, e.g., | |||
|
|||
```swift | |||
@traced(logLevel: 2) | |||
@Traced(logLevel: 2) | |||
func myFunction(a: Int, b: Int) { ... } | |||
``` | |||
|
|||
where the `traced` macro is declared as something like: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Traced
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
The Language Workgroup has approved this vision document. Merging! |
Updated and revised from the gist initially posted to the forums back in October (https://forums.swift.org/t/a-possible-vision-for-macros-in-swift/60900), reflecting improvements to the design.