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

Tino Heth 2th at gmx.de
Fri Dec 2 06:33:35 CST 2016


> 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/d7f50906/attachment.html>


More information about the swift-users mailing list