How unbox type-erasures AnyCollection or AnySequence?

Is there any way return to the original type?

    let collection = [1, 2, 3]
    let anyCollection = AnyCollection(collection)

    // ...

    if let unboxedCollection = anyCollection.unbox() as? [Int] {
      // ...
    }
extension AnyCollection {
  func unbox() -> [Element] {
    return self.map { $0 }
  }
}

let collection = [1, 2, 3] // [Int]
let anyCollection = AnyCollection(collection) // AnyCollection<Int>
let unboxedCollection = anyCollection.unbox() // [Int]

Is this what you're looking for?

1 Like