If you do that, Array
’s conformance will be with the default method. The others will only ever be called if they happen to be an available static overload in a particular context. Namely:
let point = CGPoint(x: 1, y: 2)
// This will use the more‐specific overload.
print(array.playgroundDescription)
func demonstrate<T>(_ array: [T]) {
// But this will only ever use the default implementation...
print(array.playgroundDescription)
}
demonstrate(CGPoint(point)) // ...even when you probably want the specific one.
Instead, try to design it like this:
protocol CustomArrayElementPlaygroundDisplayConvertible {
// Declare whatever options you’ll use here.
var somethingRelevant: String { get }
}
extension Array where Element : CustomArrayElementPlaygroundDisplayConvertible {
var playgroundDescription: Any {
// Construct something using CustomArrayElementPlaygroundDisplayConvertible.
return map({ $0.somethingRelevant }).joined()
}
}
extension CGPoint : CustomArrayElementPlaygroundDisplayConvertible {
// Conform...
}
extension Optional : CustomArrayElementPlaygroundDisplayConvertible
where Wrapped : CustomArrayElementPlaygroundDisplayConvertible {
// Conform...
}
But you may want to do that on a wrapper instead of directly on Array
, because there are pitfalls to creating a conformance of a protocol you do not control to a type you do not control.