'How can I have Hashable element type for an Array that is wrapped value for optional

I am looking the correct syntax of this code:

extension Optional where Wrapped == Array<Element> where Element == Hashable {

}

The elements of Array are Hashable.



Solution 1:[1]

You can either constrain every member, or use a typealias. But you can't use properties unless you use the typealias solution.

typealias OptionalHashableArray<Hashable: Swift.Hashable> = [Hashable]?

extension OptionalHashableArray {
  var ?: String { "?" }
}

([] as Optional).?
extension Optional {
  func ?<Hashable: Swift.Hashable>() -> String
  where Wrapped == [Hashable] {
    "?"
  }
}

[AnyHashable]?.none.?()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1