Allow extensions on generic types with a type constraint

Originator:gordon
Number:rdar://19905201 Date Originated:February 20, 2015
Status:Open Resolved:
Product:Developer Tools Product Version:Swift 1.2
Classification:Feature (New) Reproducible:Always
 
Summary:
It would be really useful to be able to add an extension to (for example) Array, but only when Array contains objects that conform to a specific protocol. This currently isn’t possible.

Steps to Reproduce:
1. Write the following extension:

extension Array<T: Printable> {
  func descriptions() -> [String] {
    return map { $0.description }
  }
}

2. Attempt to compile

Expected Results:
The extension compiles

Actual Results:
The extension doesn’t compile, and throws an error that extensions on generic types cannot add requirements.

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!