Swift, protocol conditional conformance, what's the difference between Element: Protocol and Element == Protocol when implement nested array?

Viewed 81

It's easier to describe it with a simple example,

protocol PlistNative { }

extension Bool   : PlistNative { }
extension String : PlistNative { }
extension Int    : PlistNative { }

Then, if we check the conditional conformance by type equality, i.e. treating the PlistNative protocol as a type,

extension Array: PlistNative where Array.Element == PlistNative { }

// success
let ary: [PlistNative] = [1, "hello", ["a", 6, ["hi"]]]

But, nested array wont' work if we treat it as a protocol, i.e. by checking if Element conforms to it,

extension Array: PlistNative where Array.Element: PlistNative { }

// Cannot convert value of type '[Any]' to expected element type 'PlistNative'
let ary: [PlistNative] = [1, "hello", ["a", 6, ["hi"]]]

Why the type equality works, but the protocol conformance do not?

0 Answers
Related