# Extensions on nested generics with protocols

**URL:** <https://forums.swift.org/t/extensions-on-nested-generics-with-protocols/85954>\
**Category:** Using Swift\
**Tags:** generics, extensions\
**Created:** [April 10, 2026, 7:48pm UTC](https://forums.swift.org/t/extensions-on-nested-generics-with-protocols/85954 "2026-04-10T19:48:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bnickel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bnickel/32/7804_2.png) [@bnickel](https://forums.swift.org/u/bnickel)\
**Post date:** [April 10, 2026, 7:48pm UTC](https://forums.swift.org/t/extensions-on-nested-generics-with-protocols/85954/1 "2026-04-10T19:48:16Z")

</div>

If I have an array where the elements conform to `MyProtocol` and want to create an extension for it, I can do something along the lines of:

```swift
extension Array where Element: MyProtocol {
  func myFunc() { ... }
}

```

Is it possible to write an extension for an array an array of arrays of `MyProtocol`? I’ve tried a few combinations, but everything results in me having to use `<>` to define the child generic and that always demands a concrete type.

Ideally, I’d be able to do something like:

```swift
extension Array where Element == Array, Element.Element: MyProtocol {
  func myFunc() { ... }
}

```

The concrete problem I’m trying to solve is that I have a generic lockable type `Lockable<T>`, which I want to use to hold an array of weak delegates, with helpers:

```swift
extension Lockable<Array<WeakHolder<T: AnyObject>>> {
  func add(_ delegate: T)
  func cullAndReturn() -> Array<T>
}

```

I’ve tried using composition instead with a type `WeakDelegateStore<Delegate: AnyObject>`, but have run into this issue on consumption: [Generic class requires that generic type be a class type](https://forums.swift.org/t/generic-class-requires-that-generic-type-be-a-class-type/72176)

I’m suspecting my only option is to have an array of `AnyObject` and have `cullAndReturn<T>()` cast the type on read.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [April 10, 2026, 8:36pm UTC](https://forums.swift.org/t/extensions-on-nested-generics-with-protocols/85954/2 "2026-04-10T20:36:33Z")

</div>

You can usually do something like this by adding generics on the extension function, since you can't do it on the extension itself.

```swift
extension Array {
    func doThing<T>() where Element == Array<T> {
        
    }
}

```

---

<div class="post-metadata">

**Author:** ![bnickel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bnickel/32/7804_2.png) [@bnickel](https://forums.swift.org/u/bnickel)\
**Post date:** [April 10, 2026, 8:49pm UTC](https://forums.swift.org/t/extensions-on-nested-generics-with-protocols/85954/3 "2026-04-10T20:49:35Z")

</div>

Thanks @Jon_Shier, I completely missed that option!

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [April 10, 2026, 9:31pm UTC](https://forums.swift.org/t/extensions-on-nested-generics-with-protocols/85954/4 "2026-04-10T21:31:13Z")

</div>

Would it suffice to constraint the elements to be collections instead of arrays? Because then you could do this:

```swift
extension Collection where Element: Collection, Element.Element: MyProtocol { … }

```
