[Swift] extension with constraints cannot have inheritance clause

Originator:martin.kuehl
Number:rdar://23433955 Date Originated:06-Nov-2015 08:17 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 7.1 (7B91b)
Classification:Enhancement Reproducible:Always
 
This is essentially a duplicate of rdar://23255436

Summary:
In some cases it would be useful to extend a generic type under some conditions for the generic argument. 

For example one could declare a protocol JSONEncodable to which all JSON types conform (e.g. Int, String, Bool, …). Each array of these types is JSON encodable as well and should also conform to JSONEncodable if it only contains elements that conform to JSONEncodable. However, writing an extension of Array like the following gives the error: Extension of type 'Array' with constraints cannot have an inheritance clause

extension Array: JSONDecodable where Element: JSONDecodable

As an additional use case, consider Equatable:

struct T: Equatable {}
let t = T()
t == t
[t] == [t]
[[t]] == [[t]]

The last statement does not compile because although Array where Element: Equatable can be compared using ==, they cannot themselves be declared Equatable, and so cannot be nested.

Steps to Reproduce:
Open the sample playground and try to compile it

Expected Results:
The code should compile and arrays should conform to Equatable if its elements are Equatable

Actual Results:
The code does not compile

Version:
Xcode 7.1 (7B91b)

Notes:

struct Thing: Equatable {}

func ==(this: Thing, that: Thing) -> Bool {
  return true
}

let it = Thing()

it == it
[it] == [it]
[[it]] == [[it]]

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!