Skip to content
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

Closed
swift-ci opened this issue May 25, 2016 · 9 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself good first issue Good for newcomers

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1612
Radar rdar://problem/18095061
Original Reporter brett (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, StarterBug
Assignee @CodaFi
Priority Medium

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:

func check(a: AnyObject) -> Bool {
    return a is CGColor
}
check("hey") --> true
check(1.0) --> true
check(UIColor.redColor()) --> true

This basically means that generalized typechecking on these structs is, as far as I can see, currently not possible.

@belkadan
Copy link
Contributor

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.

@belkadan
Copy link
Contributor

To be clear, the "StarterBug" label I attached is just for adding the diagnostic. The relevant thing to check for is a "foreign" class.

@swift-ci
Copy link
Collaborator Author

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.

@belkadan
Copy link
Contributor

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.

@belkadan
Copy link
Contributor

…so it's good to collect information about when it is the right tool for the job.

@swift-ci
Copy link
Collaborator Author

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.

@swift-ci
Copy link
Collaborator Author

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?).

@swift-ci
Copy link
Collaborator Author

Comment by Brett Walker (JIRA)

For reference, I just launched the animation library I was speaking of. MotionMachine

@CodaFi
Copy link
Member

CodaFi commented Aug 9, 2016

Resolved by the introduction of a diagnostic in #2754.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants