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-11273] Unexpected behaviour of as? operator with Any and Optional<Any> #53674

Open
swift-ci opened this issue Aug 8, 2019 · 1 comment
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Aug 8, 2019

Previous ID SR-11273
Radar None
Original Reporter Crescenzio (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 717afe7ddeedc959012564e404e0facf

Issue Description:

When an `Optional<Any>` is saved in an `Any` variable, trying to cast the the object to a protocol fails, even if the actual object is a class that conforms to that protocol.

protocol Model {} 
class ModelImpl: Model {}

let test: Any = ModelImpl() as Any? // I received from a library my object as Any but wrapped in an Optional<Any>   

let cast = test as? Model // this returns nil 
let cast2 = test as? ModelImpl // this returns an actual object

Not sure if this is a bug or an expected behaviour, but it's a bit confusing.

@belkadan
Copy link
Contributor

@jckarter, we have a dup for this, right?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

2 participants