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

Adrian Zubarev adrian.zubarev at devandartist.com
Fri Dec 2 10:29:54 CST 2016


Thanks, that works party as needed but I just realize this main idea here is exactly an example where one would need generic protocols, because I cannot overload the function with associatedtype as I’d need it.



-- 
Adrian Zubarev
Sent with Airmail

Am 2. Dezember 2016 um 13:33:37, Tino Heth (2th at gmx.de) schrieb:


Any advice how to rewrite the code so it still would do its job under the hood?
I'm not aware of any clean way to do this (with current Swift ;-) — but this hack passes compilation:

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

struct Box<B> {
var performWith: (B) -> Void

init<T: Proto>(value: T) where T.A == B {
self.performWith = value.performWith
}
}

final class SomeType<B> {
var protos: [Box<B>]

init(_ protos: [Box<B>]) {
self.protos = protos
}

func callProtosWith(_ b: B) {
self.protos.forEach {
$0.performWith(b)
}
}
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161202/ce74ada0/attachment.html>


More information about the swift-users mailing list