[swift-users] How to rewrite this snippet without `existentials`?

Adrian Zubarev adrian.zubarev at devandartist.com
Fri Dec 2 05:06:30 CST 2016


Existentials are still missing in the language. Any advice how to rewrite the code so it still would do its job under the hood?

protocol Proto : class {

     // `A` is a generic type and therefore should stay as an associatedtype
     associatedtype A
     func performWith(_ a: A)
}

final class SomeType<B> {

     typealias ProtoB = Any<Proto> where Proto.A == B

     var protos: [ProtoB]

     init(_ protos: [ProtoB]) {
          self.protos = protos
     }
      
     func callProtosWith(_ b: B) {
         
          self.protos.forEach {
            $0.performWith(b)
          }
     }
}
I could make protocol Proto : BaseProto and safe an array of BaseProto but how do I convert it back to Proto when needed, so I could access performWith(_:) function?

I cannot use the generic parameter list from SomeType to enforce this, because the class that conforms to Proto might not have the same base class, which is the intended behavior.



-- 
Adrian Zubarev
Sent with Airmail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161202/cbfad3e7/attachment.html>


More information about the swift-users mailing list