Open 54d85349-cb6a-41a9-b34d-f1337b898fac opened 7 years ago
Hm. I think the problem is that observe(_:options:changeHandler:)
checks to make sure the type of the key path matches up with the type of the receiver, and you've lost that once you cast to NSObject. But the type checker should be able to tell you that instead of complaining about Value
.
Workaround, or perhaps a more correct API anyway: declare the generic parameter as T: NSObject
and drop the cast, or put the bind
method in a constrained extension.
cc @xedin
@belkadan Alright, thank you for the info. I didn't want to limit the `T` to `NSObject` since I wanted to observe-feature to be optional, but I guess that's how it has to be done.
You can still do that with a constrained extension:
extension Row where T: NSObject {
func bind<Value>(to targetKeyPath: ReferenceWritableKeyPath<T, Value>) { … }
}
Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, DiagnosticsQoI, TypeChecker | |Assignee | @xedin | |Priority | Medium | md5: c8f5eae56fa3e05644eb87588f7c5551Issue Description:
I believe I should be able to have a generic class like below, that can listen to changes for any attribute of `T` as long as `T` is a subclass of `NSObject`
But I get this error: Generic parameter 'Value' could not be inferred
I'd use this for example like this: