-
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
[SR-2708] Extending ObjC generics in Swift 3 does not compile #45312
Comments
@jckarter, we've seen this before, right? |
Right now, there's an unfortunate restriction that the extension methods have to be |
Comment by Anton Bronnikov (JIRA) So, is this something that should be fixed, or is it by design and should be just documented? |
Probably both. |
Comment by Mathew Huusko V (JIRA) Also experiencing this (extensions that were compiling in Swift 2, stopped in Swift 3 now that the class's generics are being imported). Two things that seem to make this rather serious..
|
Comment by Vasili Silin (JIRA) Hi. By my mind, I have similar issue. Don't know how to add +1 to the issue. Let me share code with you. I have class like this: extension AWSTask {
func continueWithCompletionSource<TResult : Mappable>(_ taskCompletionSource: TaskCompletionSource<TResult>) {
self.continue({ (task: AWSTask) -> Any? in
switch (task.error, Mapper<TResult>.map(task.result)) {
case (let error, _) where error != nil:
taskCompletionSource.trySet(error: error!)
break
case (nil, let result) where result != nil:
taskCompletionSource.trySet(result: result!)
break
default:
taskCompletionSource.trySet(error: NSError.unkownContinueTask)
break
}
return nil
})
}
} But it's not work in Swift 3, because AWSTask has generic param. I use workaround for now, but hope it will be fixed in future. Classes: Best regards, |
Comment by Mathew Huusko V (JIRA) In the meantime/for Swift 3, is there any way to prevent an Obj-C class with lightweight generics being imported as generic into Swift? With `__covariant` being ignored, and extensions not possible (thus not being able to replace NS_REFINED_FOR_SWIFT symbols), integration is rather tricky/it's more trouble than it's worth.. |
We can't promise that a fix is on the way, unfortunately. @rjmccall did propose a change to the model that might save us from having to enforce these annoying constraints on ObjC generic behavior—instead of saying that the generic metadata for the type-erased parameters is never available at runtime, Swift could act as if it always is, and make a best effort at propagating it as long as Swift is in control of calling conventions. When we pass through a C or ObjC interface that necessitates dropping the runtime type info, we can revert the erased type parameters to their upper bound type ( |
Comment by Michael Armstrong (JIRA) +1 here, i've effectively "worked around" this by moving the offending code outside of an extension for now and into a standard class. commenting as I want to track the ticket. |
Comment by Jean-Luc Jumpertz (JIRA) I get the same error when trying to extend
In this case, the generic Obj-C class that cannot be extended is a standard Foundation class, which makes difficult to find satisfactory workarounds for this issue. |
Comment by Derrick Ho (JIRA) I'm experiencing this in Xcode 8.3 |
Comment by Hitesh Savaliya (JIRA) Any updates on this bug, please? I'm seeing similar issue `description` on extension of NSCache let key = "ABC"
extension NSCache {
override open var description: String {
if let value = self.object(forKey: key) { // Error: Extension of a generic Objective-C class cannot access the class's generic parameters at runtime
return value as String
}
} |
If you've moved to Swift 4, then you may need to explicitly make the extension members `@objc` in order to suppress that error, since Swift 4 no longer infers it. |
Comment by Ayush Goel (JIRA) FWIW, it happens in Xcode 8.3.3. Hope Swift 4 resolves the matter. I face it when extending |
Comment by Daisy Ramos (JIRA) +1 seeing the same error, when extending RACSignal from ReactiveObjC framework Extension of a generic Objective-C class cannot access the class's generic parameters at runtime in Swift 4.1 Xcode 9.3 |
Comment by Amol Y Patil (JIRA) Currently, I am using swift 4.2 and Xcode 10.1. I have recently updated libraries as below : Using ReactiveCocoa (9.0.0) Using ReactiveObjC (3.1.1) Using ReactiveSwift (5.0.1) Using Result (4.1.0) and I am getting the same error : Extension of a generic Objective-C class cannot access the class's generic parameters at runtime my func looks like : extension RACSignal {
} |
Comment by Amol Y Patil (JIRA) Hi Daisy, Did you find solution for this compiler error ? Thanks, |
Comment by Charlton Provatas (JIRA) Hitting this one as well � |
Environment
XCode Version 8.0 (8A218a)
Additional Detail from JIRA
md5: 2fa4d679d5ba96de70dc71c058d5ab46
relates to:
Issue Description:
ObjC:
Swift:
Both of the extension methods result in "Extension of a generic Objective-C class cannot access the class's generic parameters at runtime". However, neither really does anything like that (at least not explicitly).
The text was updated successfully, but these errors were encountered: