# Conditional Conformance Problems

**URL:** <https://forums.swift.org/t/conditional-conformance-problems/19633>\
**Category:** Using Swift\
**Created:** [January 19, 2019, 6:46pm UTC](https://forums.swift.org/t/conditional-conformance-problems/19633 "2019-01-19T18:46:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wildchild9](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wildchild9/32/9397_2.png) [@Wildchild9](https://forums.swift.org/u/Wildchild9)\
**Post date:** [January 19, 2019, 6:46pm UTC](https://forums.swift.org/t/conditional-conformance-problems/19633/1 "2019-01-19T18:46:41Z")

</div>

# Conditional Conformance Problems

I was writing code earlier to make `Array` conform to `CustomPlaygroundDisplayConvertible` when it's elements are of type `CGPoint`. I wanted to make a different conformance for an `Array` when its elements are of type `Optional<CGPoint>` and ran into the following problem:

```
extension Array: CustomPlaygroundDisplayConvertible where Element == Optional<CGPoint> {
    //...
}

// Error: Conflicting conformance of 'Array<Element>' to protocol 'CustomPlaygroundDisplayConvertible'; there cannot be more than one conformance, even with different conditional bounds
extension Array: CustomPlaygroundDisplayConvertible where Element == CGPoint {
    //...
}

```

I haven't a clue why this is happening, aren't these different types?

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [January 19, 2019, 7:05pm UTC](https://forums.swift.org/t/conditional-conformance-problems/19633/2 "2019-01-19T19:05:14Z")

</div>

Hello,

Multiple conditional conformances are currently not allowed: [swift-evolution/0143-conditional-conformances.md at master · apple/swift-evolution · GitHub](https://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md#overlapping-conformances)

In your case it looks innocuous, but there are many possible traps, as explained in the previous link. See also those related threads, for more context and information:

- [Conditional Conformance: Multiple conformances when wrapper has multiple generic elements](https://forums.swift.org/t/conditional-conformance-multiple-conformances-when-wrapper-has-multiple-generic-elements/11480)
- [Not possible to conditionally conform something to a protocol multiple times?](https://forums.swift.org/t/not-possible-to-conditionally-conform-something-to-a-protocol-multiple-times/11691)

---

<div class="post-metadata">

**Author:** ![twof](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/twof/32/1628_2.png) [@twof](https://forums.swift.org/u/twof)\
**Post date:** [January 19, 2019, 7:23pm UTC](https://forums.swift.org/t/conditional-conformance-problems/19633/3 "2019-01-19T19:23:47Z")

</div>

You may be able to get the result you're looking for by doing something like this.

```swift
extension Array: CustomPlaygroundDisplayConvertible { }

extension Array where Element == Optional<CGPoint> {
    // CustomPlaygroundDisplayConvertible implementation goes here
}

extension Array where Element == CGPoint {
    // CustomPlaygroundDisplayConvertible implementation goes here
}

extension Array {
    // Default CustomPlaygroundDisplayConvertible implementation goes here
}

```

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [January 20, 2019, 12:22am UTC](https://forums.swift.org/t/conditional-conformance-problems/19633/4 "2019-01-20T00:22:33Z")

</div>

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:

```swift
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:

```swift
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.

> [@Conditional conformance collisions](https://forums.swift.org/t/conditional-conformance-collisions/18023):
>
> I ran into a potentially nasty issue with conditional conformances recently. I was importing the SourceKit-LSP code into our (Google's) monorepo so I could set it up building with Bazel; it depends on SwiftPM, which we already have imported and also build from source. The version of SwiftPM we have is tracking fairly close to HEAD, so it's moved beyond the last 0.3.0 release. Let's assume that rolling our copy of SwiftPM back to 0.3.0 is not possible. SourceKit-LSP [extends stdlib's Range: Codab…](https://github.com/apple/sourcekit-lsp/blob/master/Sources/LanguageServerProtocol/Position.swift#L44)
