Is there a way to define an extension on an Array where the Element is a type with a generic? I know it's possible to declare an extension if I make the generic a concrete type, but what if I want to remain generic here?
E.g.
extension Array where Element == Range<Date> {
func foo() {
// Do something
}
}
Ah, this is exactly it. In fact, I even tried this proposed syntax when seeing if I could get it work. At least I know there is a name for the proposed behaviour now and I can track it. Thanks Nevin.
class Weak<Value: AnyObject> {
weak var value: Value?
init(_ value: Value) {
self.value = value
}
}
protocol Weakly {
associatedtype Value: AnyObject
var weak: Weak<Value> { get }
}
extension Weak: Weakly {
var weak: Weak<Value> { return self }
}
extension Array where Element: Weakly {
func contains(_ value: Weakly) -> Bool {
return self.flatMap( { $0.weak.value } ).contains(value)
}
}
I am trying to create a generic convenience contains() call on an array that contains Weak boxed types. Tried using your approach but couldn't quite get it how I wanted.