One way to do this in Swift is a method called type erasure.
Type erasure means you create a new type that wraps any value whose
concrete type you want to erase. This new type also conforms to the
protocol. By convention the type is named Any... (compare AnyIterator
and AnySequence in the
This sounds like the right approach!
However, as I experimented with AnyHashable more, I found out that after
converting a concrete type to it, I could still convert back using “as”:
AnyHashable(Foo()) as! Foo
I guess that’s not the case with AnyNamed? I tried to imitate AnyHashable:
struct