# Extending Set where it contains a generic type?

**URL:** <https://forums.swift.org/t/extending-set-where-it-contains-a-generic-type/27346>\
**Category:** Using Swift\
**Tags:** generics\
**Created:** [July 26, 2019, 12:40pm UTC](https://forums.swift.org/t/extending-set-where-it-contains-a-generic-type/27346 "2019-07-26T12:40:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rayascott](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rayascott/32/6894_2.png) [@rayascott](https://forums.swift.org/u/rayascott)\
**Post date:** [July 26, 2019, 12:40pm UTC](https://forums.swift.org/t/extending-set-where-it-contains-a-generic-type/27346/1 "2019-07-26T12:40:54Z")

</div>

I have this generic type:

```
struct ObjectFailure<T: Object> : Hashable {

    let object: T
    let error: Error
}

```

And I want to create a convenience method on `Set` whenever it contains an `ObjectFailure<T>` but is that actually possible with Swift? I've tried many things but can't get it to work. The extension doesn't know what `T` is obviously...

```
extension Set where Element == ObjectFailure<T> {

    func contains(object: Object) -> Bool {
        for element: Element in self {
            if element.object === object {
                return true
            }
        }
        return false
    }
}

```

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [July 26, 2019, 12:47pm UTC](https://forums.swift.org/t/extending-set-where-it-contains-a-generic-type/27346/2 "2019-07-26T12:47:42Z")

</div>

The trick is to make `contains(object:)` generic

```swift
extension Set {
    func contains<U>(object: U) -> Bool where Element == ObjectFailure<U> {
        for element: Element in self {
            if element.object == object {
                return true
            }
        }
        return false
    }
}

```

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [July 26, 2019, 12:48pm UTC](https://forums.swift.org/t/extending-set-where-it-contains-a-generic-type/27346/3 "2019-07-26T12:48:17Z")

</div>

No, you would need generic extensions for that. Hopefully we'll have them soon-ish, although possibly not in 5.1.

See this discussion for more info: [Parameterized Extensions](https://forums.swift.org/t/parameterized-extensions/25563)

EDIT: Or, yes, sink the generic constraint down to the individual additions, as @cukr suggests. Although this won't work for computed properties, which cannot be made generic.

---

<div class="post-metadata">

**Author:** ![rayascott](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rayascott/32/6894_2.png) [@rayascott](https://forums.swift.org/u/rayascott)\
**Post date:** [July 26, 2019, 12:57pm UTC](https://forums.swift.org/t/extending-set-where-it-contains-a-generic-type/27346/4 "2019-07-26T12:57:47Z")

</div>

Thank you both (@Karl) very very much! Super happy with that. :-)
