-
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
objcProtocol.Type.self fail to cast to Protocol #62134
Comments
|
Another try: |
|
One possible solution I expect is to make the type returned by aProtocol.self (aProtocol.Protocol, aka Protocol) consistent with the one in the parameter: |
|
What are you trying to do? What is the type signature of |
|
Hi @AnthonyLatsis, I know when I use aProtocol.Type.self, the complier will return me a Protocol object, and it will lose all the type information about the aProtocol, and the problem I'm having is that I want to get the Protocol object, and I want the function to be generic, so I can return the object that implements the aProtocol objonly_classConformingToProtocol takes a Protocol object and returns a Class that can be found in the registry: |
|
The protocol metatype object (the protocol type as an object) is Unfortunately, there is no way of expressing a generic parameter that accepts only protocol types, so
I wasn’t able to find any documentation on this |
|
We basically only use the Protocol object as a key to the dictionary to find the class in objonly_classConformingToProtocol, and this practice of using objonly_classConformingToProtocol is very widespread in our projects. What I mean is, can this generic be added at the compiler level. The compiler can know what I mean when I use P.Protocol like this: Or allow me to use Protocol(or protocol) as a generic constraint like: |
No, you cannot constrain a generic parameter to protocols only, Swift currently lacks this kind of expressivity. There is no analogue of
The question of whether something similar to this will work boils down to the question of how you instantiate an object of this |
|
Hi @AnthonyLatsis, I can use objonly_classConformingToProtocol in Swift directly, but the "Class" from Objc will become "AnyClass" in Swift, and for historical reasons, compiler allow me to call any Objc method with AnyClass. It's not safe, and it's not what I want |
Describe the bug
I have a method for manipulating objcProtocol that looks like this
When I call it like:
classConforming(to: aProtocol.Type.self)
Xcode shows me that aProtocol.Type.self is aProtocol.Type.Protocol and lldb tells me that objcProtocol is "@thick aProtocol.Type.Protocol" and p is "" but print will tell me it is aProtocol.Type and I can't cast it to Protocol, pp will always nil.
I also tried adding where P.Type == Protocol, but the compiler would give me an error:
I'm not sure how the compiler converts, but I think since Swift lacks a mechanism to determine whether it's a protocol/class/struct/enum/actor, it should at least ensure type consistency when passing parameters
The text was updated successfully, but these errors were encountered: