# Is it possible to automate the making of type erasure?

**URL:** <https://forums.swift.org/t/is-it-possible-to-automate-the-making-of-type-erasure/33527>\
**Category:** Using Swift\
**Created:** [February 6, 2020, 6:16pm UTC](https://forums.swift.org/t/is-it-possible-to-automate-the-making-of-type-erasure/33527 "2020-02-06T18:16:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [February 6, 2020, 6:16pm UTC](https://forums.swift.org/t/is-it-possible-to-automate-the-making-of-type-erasure/33527/1 "2020-02-06T18:16:32Z")

</div>

I'm talking about things like AnyHashable, AnyView, etc.

Ideally we shouldn't need to hand code type erase because they all seem to be very boilerplate. Can the compiler just do it by itself by making the compiler more smart?

---

<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:** [February 6, 2020, 8:33pm UTC](https://forums.swift.org/t/is-it-possible-to-automate-the-making-of-type-erasure/33527/2 "2020-02-06T20:33:10Z")

</div>

Yes. What you're talking about is called an "existential" (things like `Any` and `AnyObject`). You can do that with regular protocol types, without needing to box them in an `AnyMyProtocol` type:

```swift
protocol MyProtocol {
  func doSomething() -> Int
}

func myFunction(_ p: MyProtocol) {
  let x = p.doSomething()
}

var existential: MyProtocol = //...

```

Unfortunately, that doesn't currently work with protocols with associated types ("PAT" for short) or that use `Self`. `Hashable`, `Collection`, `View` and certain other common protocols have pre-built boxing types which emulate existentials, and as you're aware, you can also build your own. It's a known issue that will hopefully be alleviated one day by enhancing the compiler.

If you want to know more, "generalized existentials" or "PATs" are search terms you might find helpful.

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [February 7, 2020, 10:40pm UTC](https://forums.swift.org/t/is-it-possible-to-automate-the-making-of-type-erasure/33527/3 "2020-02-07T22:40:42Z")

</div>

Wait, why is there an `AnyHashable` type? `Hashable` doesn’t (currently?) have `Self`/PAT requirements, right?

---

<div class="post-metadata">

**Author:** ![jarod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jarod/32/364_2.png) [@jarod](https://forums.swift.org/u/jarod)\
**Post date:** [February 7, 2020, 10:44pm UTC](https://forums.swift.org/t/is-it-possible-to-automate-the-making-of-type-erasure/33527/4 "2020-02-07T22:44:46Z")

</div>

`Hashable` conforms to `Equatable`, which does have a `Self` requirement

---

<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:** [February 7, 2020, 11:33pm UTC](https://forums.swift.org/t/is-it-possible-to-automate-the-making-of-type-erasure/33527/5 "2020-02-07T23:33:20Z")

</div>

Also, as I recently discovered, it is not possible to implement `AnyHashable` (or `AnyEquatable`) in pure Swift.

If a class `Super` implements `Equatable`, and then it has two different sub-classes `Sub1` and `Sub2`, then there is no way to implement the `==` operator for `AnyEquatable` correctly in pure Swift.

Instances `s1: Sub1` and `s2: Sub2` which compare equal via `Super.==` still need to compare equal as `AnyEquatable(s1)` and `AnyEquatable(s2)`, but neither `Sub1` nor `Sub2` can be converted to each other, and there’s no way to find their common ancestor class which conforms to `Equatable` and implements `==`.

The standard library type `AnyHashable` is written partially in C++ specifically so that it can find that common ancestor implementing `Equatable`.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [September 30, 2020, 12:59am UTC](https://forums.swift.org/t/is-it-possible-to-automate-the-making-of-type-erasure/33527/6 "2020-09-30T00:59:29Z")

</div>

Is [Unlock Existential Types for All Protocols](https://forums.swift.org/t/unlock-existential-types-for-all-protocols/40665) "generalized existentials"?

---

<div class="post-metadata">

**Author:** ![filip-sakel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filip-sakel/32/8697_2.png) [@filip-sakel](https://forums.swift.org/u/filip-sakel)\
**Post date:** [October 12, 2020, 8:32pm UTC](https://forums.swift.org/t/is-it-possible-to-automate-the-making-of-type-erasure/33527/7 "2020-10-12T20:32:16Z")

</div>

I guess you could technically call it "generalized existentials", as it allows any protocol and protocol composition type to offer an existential type. The [generics manifesto](https://github.com/apple/swift/blob/main/docs/GenericsManifesto.md#generalized-existentials) specifically says:

_"The restrictions on existential types came from an implementation limitation, but it is reasonable to allow a value of protocol type even when the protocol has `Self` constraints or associated types"_

However, you should keep in mind that the proposal won't enable use of requirements that use associated types. For instance, this will _not_ work:

```swift
protocol Identifiable {
  associated type ID : Hashable
 
  var id: ID { get }
}

let identifiable: Identifiable = ...

identifiable.id ❌
// The requirement 'id' uses the associated type
// 'ID' which is prohibited; therefore we get an error.

```

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [October 14, 2020, 6:45pm UTC](https://forums.swift.org/t/is-it-possible-to-automate-the-making-of-type-erasure/33527/8 "2020-10-14T18:45:33Z")

</div>

From my findings, the only thing holding this back is [Unlock Existential Types for All Protocols](https://forums.swift.org/t/unlock-existential-types-for-all-protocols/40665). There's simply no way to tell the compiler that some arbitrary superclass type conforms to Equatable without being able to do something like `as! Equatable` or `if let type = superclassType as? Equatable.Type`. Once we have that, being able to write `AnyHashable` or `AnyEquatable` in pure Swift will be possible, albeit with a little reflection help.
