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-1043] Cannot Convert Array of Types Conforming to a Protocol to an Array of that Protocol Type #43655

Open
swift-ci opened this issue Mar 23, 2016 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-1043
Radar None
Original Reporter Ben-G (JIRA User)
Type Bug
Status Reopened
Resolution
Environment

Xcode 7.3, Swift 2.2

Additional Detail from JIRA
Votes 11
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 5933d8df40f388055dd4c8c47d23ca7e

relates to:

  • SR-352 Crash when implicitly converting array of [T] to a protocols [SomeProtocol]

Issue Description:

Swift currently cannot convert an array of types conforming to a protocol X to an array of [X]. It is possible to circumvent this by using `map`.

Example:

protocol AnotherThing {}

struct X: AnotherThing {}

let a = [X(), X(), X()]

func takesX(a: [X]) -> [AnotherThing] {
    // Compilation of following line fails
    let anotherThings: [AnotherThing] = a as [AnotherThing]
    //  This works:
    //  let anotherThings: [AnotherThing] = a.map { $0 as AnotherThing }

    return anotherThings
}

func takesOneX(a: X) -> AnotherThing {
    return a
}

The example project is also available on GitHub: https://github.com/Ben-G/AppleBugProjects/blob/master/ProtocolArrayTypeConversion/ProtocolArrayTypeConversion/main.swift

@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 itself
Projects
None yet
Development

No branches or pull requests

1 participant