-
Notifications
You must be signed in to change notification settings - Fork 10.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
[Property wrappers] Prototype support for enclosing self-based access #25884
[Property wrappers] Prototype support for enclosing self-based access #25884
Conversation
Allow property wrapper types to support a second access pattern for instance properties of classes. When supported, the property wrapper's static subscript(_enclosingInstance:storage:) is provided with the enclosing "self" and a reference-writable key path referring to the backing storage property. Implements rdar://problem/52222560.
@swift-ci please smoke test |
@swift-ci please build toolchain |
Linux Toolchain (Ubuntu 16.04) Install command |
macOS Toolchain Install command |
Extend handling of enclosing-self subscripts by differentiating between the original wrapped property (which now goes through `subscript(_enclosingInstance:wrapped:storage:)`) and the projected property (which goes through `subscript(_enclosingInstance:projected:storage:)`). The new middle argument provides a key path to the property that was accessed, allowing one to distinguish the property being updated.
@swift-ci please smoke test |
1 similar comment
@swift-ci please smoke test |
@swift-ci please build toolchain |
1 similar comment
@swift-ci please build toolchain |
Linux Toolchain (Ubuntu 16.04) Install command |
macOS Toolchain Install command |
@@ -4452,6 +4452,9 @@ ERROR(property_wrapper_type_requirement_not_accessible,none, | |||
"more restrictive access than its enclosing property wrapper type %3 " | |||
"(which is %select{private|fileprivate|internal|public|open}4)", | |||
(AccessLevel, DescriptiveDeclKind, DeclName, Type, AccessLevel)) | |||
ERROR(property_wrapper_ambiguous_enclosing_self_subscript, none, | |||
"property wrapper type %0 has multiple enclosing-self subscripts %1", | |||
(Type, DeclName)) |
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.
Does it work if you have something like this, to handle a value-semantics wrapper type?
// Read only access from a read only base
subscript(_enclosingInstance: EnclosingSelf, storage: KeyPath<EnclosingSelf, Self>) -> Wrapped { get }
// Writable projection from a writable base
subscript(_enclosingInstance: inout EnclosingSelf, storage: WritableKeyPath<EnclosingSelf, Self>) -> Wrapped { get set }
/// Describes the information needed to perform property wrapper access via | ||
/// the enclosing self. | ||
struct EnclosingSelfPropertyWrapperAccess { | ||
/// The (genreric) subscript that will be used to perform the access. |
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.
typo
Any tests? |
This appears to be a pretty substantial expansion of the property wrappers feature, which isn't yet standardized. It seems like a really nice extension, but are you planning to go through swift-evolution with this? |
Um. Is there a problem with the Swift toolchain in Xcode 11 GM? I have replicated the code from the Unit Tests in this pull request into a test project. As coded, with the declaration of the Observable property wrapper and MyType test class in the same source file it all works spiffingly but the moment I move the definition of the MyType class to another source file, mimicking defining the property wrapper in a utility file and then Xcode spits out a Segmentation fault 11 and will not compile. If you can only use property wrappers with the so useful enclosing self subscript creation in the file in which you define them then that's going to mean a return to boiler plate for me. I was sooooo looking forward to using this to finally create the thread safe lazy that I've been boiler plating around for the last 3 years! Segfault and the test project linked: |
@andyj-at-aspin Please file a bug at bugs.swift.org, if you haven't already. Thanks for reporting this! |
Done |
Allow property wrapper types to support a second access pattern for
instance properties of classes. When supported, the property wrapper's
static subscript(_enclosingInstance:storage:) is provided with the
enclosing "self" and a reference-writable key path referring to the
backing storage property.
Implements rdar://problem/52222560.