Cannot extend generic type where placeholder is another generic type

Viewed 61

I want to extend an Array where the Element is a nested class of a generic type:

class GenericType<T: ExpressibleByInt> {
    class NestedClass {
    }
}

extension Array where Element == GenericType.NestedClass {   // Fails: Reference to generic type 'GenericType' requires arguments in <...>.
...
}

Is there a syntax for expressing an extension for a generic where the generic's element is the nested class of another generic?

1 Answers

There is no way to express this at the extension level. You have to do it at the method/subscript/initializer level. (So you're out of luck for properties, unfortunately.)

extension Array {
  func ƒ<T>() where Element == GenericType<T>.NestedClass { }

  subscript<T>(_: T.Type = T.self) -> Void
  where Element == GenericType<T>.NestedClass {
    get { }
  }

  init?<T>() where Element == GenericType<T>.NestedClass {
    nil
  }
}
Related