Extension Array where Element == Range<String.Index> { // <== how to generalize to any kind of Range<X>, not just <String.Index>?

extension Array where Element == Range<String.Index> {  // <== how to generalize to any kind of Range<X>, not just String.Index?
    // want to let my extension handle any Range<T>
}

I’m probably overlooking something more direct, but at the very least this ought to work:

protocol RangeProtocol {
  // List whichever of Range’s members matter.
}
extension Range: RangeProtocol {}

extension Array where Element: RangeProtocol {
  // ...
}
1 Like

There's not actually a better way in today's Swift, but @Alejandro has been working to improve that! Hopefully the core team will be ready for "Parameterized Extensions" soon.

7 Likes