Extension on Array where Element is generic type

The workaround for this kind of thing is to make a protocol that can produce the parameterized type that you want, like so:

protocol Rangey {
  associatedtype Bound: Comparable
  var range: Range<Bound> { get }
}

extension Range: Rangey {
  var range: Range<Bound> { return self }
}

extension Array where Element: Rangey, Element.Bound == Date {
  func contains(date: Date) -> Bool {
    return contains(where: { $0.range.contains(date) })
  }
}
7 Likes