# Function-local generic constraints/casting

**URL:** <https://forums.swift.org/t/function-local-generic-constraints-casting/43478>\
**Category:** Using Swift\
**Created:** [January 1, 2021, 12:37am UTC](https://forums.swift.org/t/function-local-generic-constraints-casting/43478 "2021-01-01T00:37:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wiresoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wiresoft/32/10976_2.png) [@wiresoft](https://forums.swift.org/u/wiresoft)\
**Post date:** [January 1, 2021, 12:37am UTC](https://forums.swift.org/t/function-local-generic-constraints-casting/43478/1 "2021-01-01T00:37:49Z")

</div>

I recently ran into a situation where I had a generic type with a protocol constraint, and I needed to dynamically convert it to a generic type with a different constraint if possible. I believe there is sufficient metadata that this could be done at runtime, but I could not figure out how to express it.

Example:

```swift
// API consumers can only see this function. I do not control the API,
// so I can’t expose my fastPath() function
public func encode<T>(_ value: T) throws where T: Encodable {
    If let v = value as? T: MyProtocol {
        fastPath(v)
    } else {
        // ...
    }
}
internal func fastPath<T>(_ value: T) where T: MyProtocol {
    // ...
}

```

For reasons, my fastPath function can’t accept an existential, it has to be generic (otherwise this would be easy).  
I need a way to dynamically convert from a concrete type T: Encodable to a concrete type T: MyProtocol.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [January 1, 2021, 1:39am UTC](https://forums.swift.org/t/function-local-generic-constraints-casting/43478/2 "2021-01-01T01:39:48Z")

</div>

It sounds like you want dynamic dispatch to a non-existential protocol which is not part of the static type information at the call site.

There is, technically, a way to do this. It’s not pretty, it’s not obvious, and it wasn’t designed intentionally, but it does exist.

See the links in [this comment](https://forums.swift.org/t/how-do-i-check-if-a-type-instance-conforms-to-a-protocol-that-has-self-or-associated-type-requirements/43388/3) for details. No guarantees about runtime performance though.

---

<div class="post-metadata">

**Author:** ![rhx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rhx/32/14647_2.png) [@rhx](https://forums.swift.org/u/rhx)\
**Post date:** [January 1, 2021, 1:47am UTC](https://forums.swift.org/t/function-local-generic-constraints-casting/43478/3 "2021-01-01T01:47:17Z")

</div>

Can you specialise your `encode` function? In this case, you could do

```
public func encode<T>(_ value: T) throws where T: MyProtocol, T: Encodable {
        fastPath(v)
}

public func encode<T>(_ value: T) throws where T: Encodable {
        // ...
}

```

---

<div class="post-metadata">

**Author:** ![wiresoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wiresoft/32/10976_2.png) [@wiresoft](https://forums.swift.org/u/wiresoft)\
**Post date:** [January 1, 2021, 2:21am UTC](https://forums.swift.org/t/function-local-generic-constraints-casting/43478/4 "2021-01-01T02:21:27Z")

</div>

Right, I'm not expecting this to work statically because there is not enough information at the call site. I thought that at runtime the list of protocols a type conforms to was dynamically knowable, so it should be possible to cast a `T: Foo` to a `T: Bar` if the underlying type T conforms to both Foo and Bar even if the Bar conformance is not _statically_ known at the call site.

Thank you for the link! I will read up on it, but it's not something I can understand at a glance 😰

---

<div class="post-metadata">

**Author:** ![wiresoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wiresoft/32/10976_2.png) [@wiresoft](https://forums.swift.org/u/wiresoft)\
**Post date:** [January 1, 2021, 2:28am UTC](https://forums.swift.org/t/function-local-generic-constraints-casting/43478/5 "2021-01-01T02:28:29Z")

</div>

Unfortunately function specializations won't work in this case because this is inside the implementation of an `UnkeyedEncodingContainer`. The `Encoder` API vends an _existential_ of `UnkeyedEncodingContainer` so my specializations could never be called because they are not part of the protocol.

I could dynamically cast the container existential in all of my implementations of `encode(to:)` to check if it's this one, but that won't work for Encodable types that I don't control such as Array, etc.

---

<div class="post-metadata">

**Author:** ![wiresoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wiresoft/32/10976_2.png) [@wiresoft](https://forums.swift.org/u/wiresoft)\
**Post date:** [January 1, 2021, 2:46am UTC](https://forums.swift.org/t/function-local-generic-constraints-casting/43478/6 "2021-01-01T02:46:02Z")

</div>

Side note: if we ever re-do `Codable` it would be nice if it used opaque types instead of existentials.
