-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Protocol types with class adoption limitation in generics #61296
Comments
Ignore, I misunderstood the question. |
If I replace T.Type with Any.Type then I will lose the ability to specify a concrete type using the function parameter. And again, what confuses me is that with swift 5.6 the code is compiled and works exactly as it should. |
In this thread on the Swift forum, the author faced the same problem. |
Ah okay, I should've looked at it more closely, sorry. |
So I think this error is happening because you have a We do have an implicit mechanism to "open" the existential box, but I am unsure if it applies here. |
Thanks for your reply. I studied the proposals. I understood the concept of existential types, but I wonder why this code works in swift 5.6, which already contains existential types. And is it possible to somehow write the same code that would work in swift 5.7+ |
This was a type safety hole. You should not be able to pass a protocol metatype class C {
class func test() {
print("Hello, World!")
}
}
protocol P: C {}
func f1<T: C>(_: T.Type) {
T.test()
}
f1(P.self) What you can pass is an existential metatype func f2(_ p: P.Type) {
f1(p)
}
class Sub: C, P {}
f2(Sub.self) // Hello, World!
This is also why you cannot do |
Seems like in Swift 5.7 protocol types with class adoption limitation no longer acts like actual class type in generics
Actial behavior
The code does not compiles with error: Instance method 'subviews(ofType:)' requires that 'any Colorable' inherit from 'AnyObject'
Expected behavior
The code compiles and correctly finds the subviews of the specified type.
Environment
Additional context
That exact code compiles and works fine with Xcode 13.3.1
The text was updated successfully, but these errors were encountered: