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-5676] Can't downcast custom generics structs, only arrays #48246

Open
swift-ci opened this issue Aug 11, 2017 · 1 comment
Open

[SR-5676] Can't downcast custom generics structs, only arrays #48246

swift-ci opened this issue Aug 11, 2017 · 1 comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-5676
Radar None
Original Reporter mhuusko5 (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Improvement, LanguageFeatureRequest
Assignee None
Priority Medium

md5: 9017f57f186bfe5c62b488e8053e40a6

relates to:

  • SR-11682 Inconsistent inheritance behavior for generic classes.

Issue Description:

protocol Pet: class {}
protocol Dog: Pet {}

let pets: [Pet] = ...

if let pets = pets as? [Dog] { .. } // works

struct Food<T> { }

let food: Food<Pet> = ...

if let food = food as? Food<Dog> { .. } // doesn't work

Perhaps the same kind of mapping could be applied to custom types automatically, or at least machinery exposed that custom types can buy into to get this syntax.

@belkadan
Copy link
Contributor

Yep. Nothing in Swift is covariant at the moment besides the primary collection types, because it isn't always correct to allow downcasting (or upcasting). A simple example is a struct that refers to a reference-semantics NSArray, where it assumes everything in the array matches the generic type.

@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
compiler The Swift compiler in itself feature A feature request or implementation improvement
Projects
None yet
Development

No branches or pull requests

2 participants