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-1612] Type comparison of certain CG types with AnyObject object is always true #44221
Comments
This is expected. The right way to check a CF type is by getting its type ID, but we don't necessarily know how to do that for arbitrary CF types. We might come up with a way to do that in the future, but meanwhile we should probably warn when we can. |
To be clear, the "StarterBug" label I attached is just for adding the diagnostic. The relevant thing to check for is a "foreign" class. |
Comment by Brett Walker (JIRA) Hi, thanks for the clarification. The surprise for me came from an inconsistency in this behavior, as CGPoint, CGSize, CGRect, and CGVector all work correctly with type checking, yet other CF types like CGColor and CGImage fail. I suppose that's because the ones that do are all structs. I can work around this in my own code in an inelegant way for now, but I think that if Swift will continue to depend on CF types, those types should have stronger support in the language. It makes it hard to create generalized code when certain foundational classes aren't even detected. |
Can I ask what you need CF downcasting for? Most of the time explicitly checking for types isn't a great way to solve a problem anyway. |
…so it's good to collect information about when it is the right tool for the job. |
Comment by Brett Walker (JIRA) Sure. I'm just finishing up a new animation library that takes a compositional approach to value support. Each type "assistant" adopts a central assistant protocol, which my main animation class uses to pass values along to for manipulation. This way people can customize the types they need, or add new ones seamlessly. I'd like to add support for CGColor and CGPath, and this is where I ran into the type-checking issue, as the protocol uses AnyObject to pass objects back and forth and 'is' type checking to determine which assistant can handle the type. So that's a problem for me, as these types won't work in this system. |
Comment by Brett Walker (JIRA) I'm not sure I answered your central question though – the reason for CGColor is because some CALayer properties use this. I might be able to get around the CGPath issue by requiring UIBezierPath instead, but I think there's people that would like to deal with CGPath directly, for instance those that don't have access to UIKit (Linux?). |
Comment by Brett Walker (JIRA) For reference, I just launched the animation library I was speaking of. MotionMachine |
Resolved by the introduction of a diagnostic in #2754. |
Additional Detail from JIRA
md5: b65c6f5fa8a6d23374d8ff5142c383ba
Issue Description:
When two objects are compared using 'is', if one object is typed as AnyObject and the object being compared against is a CGColor or CGPath, the comparison seems to always be true, no matter what the type of the AnyObject object actually is. There may be more structs that fail in this comparison.
Example code:
This basically means that generalized typechecking on these structs is, as far as I can see, currently not possible.
The text was updated successfully, but these errors were encountered: