Can a generic item depend on a generic template instead of a generic type?

Here's what I want to do:

extension Sequence<T> where Element == SortedSetElement<T> {
  //...
}

I get an error that T is not in scope.

The first generic depends on a specific instantiation of the second, but the first generic's parameters depend on the second generic's parameters.

1 Like

I used an extension to switch the order the dependencies are specified:

extension SortedSetElement {
  struct MySequence<Base: Sequence> where Base.Element == SortedSetElement {
    //...
  }
}

(You can't use "Self" at the end of the second line, because it's an alias for "MySequence" instead by that point.)

You can't yet do it at the extension level, but if the only things you're adding are methods, initializers, or subscripts, you can do it by moving the where clause to the member instead:

extension Sequence {
  func doSomethingWithSortedSets<T>()
  where Element == SortedSetElement<T> {
    // ...
  }
}
4 Likes