Skip to content

Some issues in "Opaque and Boxed Protocol Types" chapter #364

@theundergroundsorcerer

Description

@theundergroundsorcerer

Location

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/opaquetypes

Description

I've found the following issues.

  1. all the the examples with boxed protocol result in the following warning.
Use of protocol 'Container' as a type must be written 'any Container'; this will be an error in a future Swift language

Obviously these code snippets have to be corrected.

  1. The second last example that should not compile at the end of Differences Between Opaque Types and Boxed Protocol Types](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/opaquetypes#Differences-Between-Opaque-Types-and-Boxed-Protocol-Types) section
// Error: Protocol with associated types can't be used as a return type.
func makeProtocolContainer<T>(item: T) -> Container {
    return [item]
}

compiles just fine, and results in the same output as the last example in the section, namely:

func makeProtocolContainer<T>(item: T) -> Container {
    return [item]
}

let boxedContainer = makeProtocolContainer(item: 12)
let twelve = boxedContainer[0]
print(type(of: twelve))
// Prints "Int"

Correction

For the first issue, obviously, any needs to be added before the boxed types.

For the second issue, I don't know whether it is an error in the compiler (namely, protocols with associated types can't be used as return types and it compiles despite it) or an error in the text (perhaps was a limitation in the past and it has been lifted??). If there's no error in the compiler, this example needs to be fixed with the adequate explanations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions