You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
there is no official way to access the "enclosing" object from withing a property wrapper. Some workaround is possible with undocumented api (see below)
when actually using the propertyWrapper (@Dependency in my example above), you cannot reference self, as it's not yet available. Therefore, all closures used for registration cannot use variables that may have been stored in self, and self should be passed to the closure itself
To pass self to the closure, we should define the enclosing type upon PW creation itself, which sounds awful and superfluous.
A possible implementation (for future reference) is:
@propertyWrapperstructDependency<Value,Container:Boomerang.DependencyContainer>where Container.DependencyKey ==ObjectIdentifier{privateletclosure:(Container)->Valueprivateletscope:Boomerang.Container<ObjectIdentifier>.Scopeinit(_ containerType:Container.Type=Container.self,
scope:Boomerang.Container<ObjectIdentifier>.Scope=.singleton,
closure:@escaping(Container)->Value){self.scope = scope
self.closure = closure
}@available(*, unavailable, message:"This property wrapper can only be applied to classes")publicvarwrappedValue:Value{get{fatalError()}// swiftlint:disable unused_setter_valueset{fatalError()}}publicstatic subscript(
_enclosingInstance instance:Container,
wrapped wrappedKeyPath:ReferenceWritableKeyPath<Container,Value>,
storage storageKeyPath:ReferenceWritableKeyPath<Container,Self>)->Value{get{
guard let value = instance.resolve(Value.self)else{
instance.register(for:Value.self,
scope:instance[keyPath: storageKeyPath].scope,
handler:{instance[keyPath: storageKeyPath].closure(instance)})return instance.unsafeResolve()}return value
}set{}}}
The text was updated successfully, but these errors were encountered:
It would be nice (?) to create a
@propertyWrapper
around dependencies when using a dependency container.Something like:
As of today the main issues are:
@Dependency
in my example above), you cannot referenceself
, as it's not yet available. Therefore, all closures used for registration cannot use variables that may have been stored in self, and self should be passed to the closure itselfA possible implementation (for future reference) is:
The text was updated successfully, but these errors were encountered: