<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Thanks for the code sample and link, but if I’m not wrong, this pattern doesn’t allow heterogeneous items.</div><div class=""><br class=""></div><div class="">If I have these definitions:</div><div class=""><br class=""></div>struct&nbsp;Chicken {}<br class="">struct&nbsp;Pig {}<br class=""><br class="">class&nbsp;ChickenFarm:&nbsp;Farm&nbsp;{<br class="">&nbsp; &nbsp;&nbsp;func&nbsp;grow() -&gt;&nbsp;Chicken&nbsp;{<br class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;Chicken()<br class="">&nbsp; &nbsp;&nbsp;}<br class="">}<br class=""><br class="">class&nbsp;PigFarm:&nbsp;Farm&nbsp;{<br class="">&nbsp; &nbsp;&nbsp;func&nbsp;grow() -&gt;&nbsp;Pig&nbsp;{<br class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;Pig()<br class="">&nbsp; &nbsp;&nbsp;}<br class="">}<br class=""><div class=""><div class=""><br class=""></div><div class="">Then:</div><div class=""><br class=""></div><div class="">var&nbsp;farms =&nbsp;// How do I define a set that can contain both ChickenFarm and PigFarm?<br class="">farms.insert(AnyFarm&lt;Chicken&gt;(ChickenFarm()))<br class="">farms.insert(AnyFarm&lt;Pig&gt;(PigFarm()))</div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 17 Jul 2017, at 4:02 AM, Nevin Brackett-Rozinsky &lt;<a href="mailto:nevin.brackettrozinsky@gmail.com" class="">nevin.brackettrozinsky@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">The standard pattern for type-erasure in Swift looks like this:<div class=""><br class=""></div><div class=""><div class=""><font face="monospace, monospace" class="">protocol Farm {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; associatedtype Produce</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; func grow() -&gt; Produce</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">private class _AnyFarmBase&lt;T&gt; : Farm {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; func grow() -&gt; T { fatalError() }</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">private final class _AnyFarmBox&lt;U: Farm&gt;: _AnyFarmBase&lt;U.Produce&gt; {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; var farm: U</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; init(_ x: U) { farm = x }</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; override func grow() -&gt; U.Produce {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; return farm.grow()</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; }</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">public final class AnyFarm&lt;V&gt; : Farm {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; private let wrapped: _AnyFarmBase&lt;V&gt;</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; func grow() -&gt; V { return wrapped.grow() }</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; init&lt;W: Farm&gt; (_ x: W) where W.Produce == V {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; &nbsp; &nbsp; wrapped = _AnyFarmBox(x)</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; &nbsp; }</font></div><div class=""><font face="monospace, monospace" class="">}</font></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">There is one little hiccough when you need an initializer in the abstract base class, which you can read about <a href="https://www.bignerdranch.com/blog/breaking-down-type-erasures-in-swift/" class="">here</a> among other places.</div><div class=""><br class=""></div><div class="">Hope that helps,</div><div class=""><br class=""></div><div class="">Nevin</div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sun, Jul 16, 2017 at 12:32 AM, Glen Huang via swift-users <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This sounds like the right approach!<br class="">
<br class="">
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”:<br class="">
<br class="">
AnyHashable(Foo()) as! Foo<br class="">
<br class="">
I guess that’s not the case with AnyNamed? I tried to imitate AnyHashable:<br class="">
<br class="">
struct AnyNamed: Named {<br class="">
&nbsp; &nbsp; let base: Any<br class="">
<span class="">&nbsp; &nbsp; init&lt;T: Named&gt;(_ value: T) {<br class="">
</span>&nbsp; &nbsp; &nbsp; &nbsp; base = value<br class="">
&nbsp; &nbsp; }<br class="">
<br class="">
&nbsp; &nbsp; var name: String {<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; // How do I convert `base` to `Named` here?<br class="">
&nbsp; &nbsp; }<br class="">
}<br class="">
<br class="">
But I have no idea what to put in `var name: String`. Also, even if we managed to come up with a solution, would it magically allow direct casting with “as”? Does the complier do something special for AnyHashable?<br class="">
<div class="HOEnZb"><div class="h5"><br class="">
<br class="">
&gt; On 16 Jul 2017, at 12:58 AM, Ole Begemann &lt;<a href="mailto:ole@oleb.net" class="">ole@oleb.net</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; One way to do this in Swift is a method called type erasure.<br class="">
&gt;<br class="">
&gt; 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 standard library, which do the same thing).<br class="">
&gt;<br class="">
&gt; struct AnyNamed: Named {<br class="">
&gt;&nbsp; &nbsp; private let _name: () -&gt; String<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; init&lt;T: Named&gt;(_ value: T) {<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; _name = { <a href="http://value.name/" rel="noreferrer" target="_blank" class="">value.name</a> }<br class="">
&gt;&nbsp; &nbsp; }<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; var name: String {<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; return _name()<br class="">
&gt;&nbsp; &nbsp; }<br class="">
&gt; }<br class="">
&gt;<br class="">
&gt; AnyNamed is initialized with a generic value T: Named. Notice that the initializer is generic, but the type itself isn't. Because AnyNamed can't store value: T directly (then it would have to be generic over T), we create a closure over <a href="http://value.name/" rel="noreferrer" target="_blank" class="">value.name</a> and store that instead.<br class="">
&gt;<br class="">
&gt; Now we can create a Set&lt;AnyNamed&gt; and, because AnyNamed conforms to Named, treat the set's elements as values conforming to Named:<br class="">
&gt;<br class="">
&gt; var set = Set&lt;AnyNamed&gt;()<br class="">
&gt; set.insert(AnyNamed(Foo()))<br class="">
&gt; set.insert(AnyNamed(Bar()))<br class="">
&gt;<br class="">
&gt; for element in set {<br class="">
&gt;&nbsp; &nbsp; print(<a href="http://element.name/" rel="noreferrer" target="_blank" class="">element.name</a>)<br class="">
&gt;&nbsp; &nbsp; print(element.hashValue)<br class="">
&gt; }<br class="">
&gt;<br class="">
&gt;<br class="">
&gt; On 11.07.2017 12:10, Glen Huang via swift-users wrote:<br class="">
&gt;&gt; Hi,<br class="">
&gt;&gt;<br class="">
&gt;&gt; I want to store some heterogeneous items all conform to a protocol inside a set, is it something possible to do in swift?<br class="">
&gt;&gt;<br class="">
&gt;&gt; I tried this example:<br class="">
&gt;&gt;<br class="">
&gt;&gt; ```<br class="">
&gt;&gt; protocol Named: Hashable {<br class="">
&gt;&gt;&nbsp; &nbsp; var name: String { get }<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; extension Named {<br class="">
&gt;&gt;&nbsp; &nbsp; var hashValue: Int {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; return name.hashValue<br class="">
&gt;&gt;&nbsp; &nbsp; }<br class="">
&gt;&gt;<br class="">
&gt;&gt;&nbsp; &nbsp; static func ==(lhs: Self, rhs: Self) -&gt; Bool {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp; &nbsp; return <a href="http://lhs.name/" rel="noreferrer" target="_blank" class="">lhs.name</a> == <a href="http://rhs.name/" rel="noreferrer" target="_blank" class="">rhs.name</a><br class="">
&gt;&gt;&nbsp; &nbsp; }<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; struct Foo: Named {<br class="">
&gt;&gt;&nbsp; &nbsp; var name = "foo"<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; struct Bar: Named {<br class="">
&gt;&gt;&nbsp; &nbsp; var name = "bar"<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; var item = Set&lt;Named&gt;()<br class="">
&gt;&gt; item.insert(Foo())<br class="">
&gt;&gt; item.insert(Bar())<br class="">
&gt;&gt; ```<br class="">
&gt;&gt;<br class="">
&gt;&gt; But it failed at `Set&lt;Named&gt;()` where it complained "Using 'Named' as a concrete type conforming to protocol 'Hashable' is not supported”.<br class="">
&gt;&gt;<br class="">
&gt;&gt; After watching the WWDC session "Protocol-Oriented Programming in Swift” by Dave Abrahams, I try to use protocols whenever possible. But I can’t seem to overcome this barrier. Set.Element must confirm to Hashable, which inherits from Equatable, which has self requirement, which ultimately means that Set.Element all must be of the same type. So it seems it’s impossible to have heterogeneous items using protocol. Is that the case?<br class="">
&gt;&gt;<br class="">
&gt;&gt; My use case is this:<br class="">
&gt;&gt;<br class="">
&gt;&gt; I have an object that can contain two sets of other objects:<br class="">
&gt;&gt;<br class="">
&gt;&gt; ```<br class="">
&gt;&gt; class Parent {<br class="">
&gt;&gt;&nbsp; &nbsp; var foos: Set&lt;Foo&gt;<br class="">
&gt;&gt;&nbsp; &nbsp; var bars: Set&lt;Bar&gt;<br class="">
&gt;&gt; }<br class="">
&gt;&gt; ```<br class="">
&gt;&gt;<br class="">
&gt;&gt; I want to define a computed property “all” that is the union of the two sets. Foo and Bar conform to the same protocol. I wonder what return type I should use for the union? Do I have to go back to OOP and define a super class for Foo and Bar?<br class="">
&gt;&gt;<br class="">
&gt;&gt; Thanks.<br class="">
&gt;&gt; ______________________________<wbr class="">_________________<br class="">
&gt;&gt; swift-users mailing list<br class="">
&gt;&gt; <a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
&gt;<br class="">
&gt;<br class="">
<br class="">
______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
</div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></body></html>