Function Array <Optional <T >> -> Optional <Array <T >>

Hier ist, was ich versuche zu tun:

extension Array<Optional<T>> {   
  func unwrap() -> Optional<Array<T>> {
    let a = self.flatMap() { a in
      switch a {
      case Optional.Some(let x): return [x]
      case Optional.None: return []
      }
    }
    if a.count == self.count {
      return Optional.Some(a)
    } else {
      return Optional.None
    }   
  } 
}

Aber es kompiliert nicht mit FehlerUse of undeclared type T. So möchte ich es verwenden:

let a = [Optional.Some(2), Optional.Some(3), Optional.Some(4)]
let b = [Optional.Some(1), Optional.None]
a.unwrap() // Optional[2, 3, 4]
b.unwrap() // nil

Wie komme ich damit zurecht? Oder gibt es in swift keine solche Möglichkeit?

Antworten auf die Frage(6)

Ihre Antwort auf die Frage