-
Notifications
You must be signed in to change notification settings - Fork 440
Add an accessor macro example #2565
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
Conversation
fd25603
to
295068d
Compare
295068d
to
b7252d2
Compare
@swift-ci Please test |
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 for adding the example 👍🏽
|
||
return [ | ||
""" | ||
get { self[\(raw: argument.expression)] } |
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.
I don’t think you need to use raw:
here or in the setter.
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.
Fixed: a283c96
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftUI |
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.
The example won’t compile on Linux and Windows if it depends on SwiftUI
.
We’ve got two options here: Either wrap the macro in #if canImport(SwiftUI)
or change it so it doesn’t depend on SwiftUI
. Which one would you prefer?
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.
Thank you for proposing two options!
I'll choose the #if canImport(SwiftUI)
option this time 🙏
@@ -47,3 +47,7 @@ runMemberMacrosPlayground() | |||
// MARK: - Peer Macros | |||
|
|||
runPeerMacrosPlayground() | |||
|
|||
// MARKL - Accessor Macros |
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.
// MARKL - Accessor Macros | |
// MARK: - Accessor Macros |
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.
Fixed 🙈 : aaefc0e
private struct MyEnvironmentKey: EnvironmentKey { | ||
static let defaultValue: String = "Default value" | ||
} | ||
|
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.
Since the expansion of the macro is syntactic and doesn’t actually try to resolve MyEnvironmentKey
, you could remove the declaration of MyEnvironmentKey
.
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.
Fixed: 74a3d68
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.
Thank you. Looks good to me 👍🏽
@swift-ci Please test |
@swift-ci Please test Windows |
Issue: #2169
This PR introduces a new accessor macro example called
EnvironmentValueMacro
that automates the generation of getter / setter implementations for EnvironmentValues.