[Solved] Error when conforming Array to ExpressibleByStringLiteral

I'm trying to conform Array to ExpressibleByStringLiteral:

extension Array: ExpressibleByStringLiteral where Element: ExpressibleByStringLiteral, Element.StringLiteralType == String {
    public init(stringLiteral value: String) {
        if value.isEmpty {
            self = []
        } else {
            let literals = value.components(separatedBy: " ")
            self = literals.map { Element(stringLiteral: $0) }
        }
    }
}

and see these errors:

Conditional conformance of type 'Array' to protocol 'ExpressibleByStringLiteral' does not imply conformance to inherited protocol 'ExpressibleByExtendedGraphemeClusterLiteral'
Conditional conformance of type 'Array' to protocol 'ExpressibleByStringLiteral' does not imply conformance to inherited protocol 'ExpressibleByUnicodeScalarLiteral'

After checking out document, I know ExpressibleByStringLiteral inherits from 'ExpressibleByExtendedGraphemeClusterLiteral' and 'ExpressibleByUnicodeScalarLiteral'. But why do I not see these errors when conforming my custom types to 'ExpressibleByStringLiteral'?

There is a difference in these two declarations:

struct Foo: ExpressibleByStringLiteral {
  …
}

struct Bar<T> {
  …
}

extension Bar: ExpressibleByStringLiteral
where T == Character {
  …
}

For Bar, the compiler can’t be sure that you want to restrict the other two protocols to also be where T == Character, so it requires you to explicitly write separate extensions for the other two protocols. They have default implementations where Self: ExpressibleByStringLiteral, so they can be empty extensions, but they must exist.

1 Like

So it's a general requirement that conditional conformance to a child protocol requires conditional conformance to all its parent protocols explicitly? I have used swift for a few years but this is the first time I know this. Thanks!

Yes, because you must be explicit about the conditions for those conditional conformances, which often are not the same.

1 Like