<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="">Thanks for bringing AnyHashable to my attention.<div class=""><br class=""></div><div class="">It works, but the types are now erased. I want to have a union of the two sets because I want to loop over it to treat each contained item as Named, so I can process them as though they are of the same type. Is this type of use case really should be addressed using super class?</div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 11 Jul 2017, at 7:38 PM, Howard Lovatt <<a href="mailto:howard.lovatt@gmail.com" class="">howard.lovatt@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class="">You can have a set of AnyHashable:<div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">var item = Set<AnyHashable>()<br class="">item.insert(AnyHashable(Foo()))<br class="">item.insert(AnyHashable(Bar()))</span></font></blockquote><div class=""><br class=""></div>Depends what you will do with the set if this is viable or not. You can also use classes and ObjectID.</div><div class=""><br class=""></div><div class="">You might want this though:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">var item = [AnyHashable: Any]<br class=""></span></font></blockquote>extension Dictionary where Key == AnyHashable, Value: Hashable {</div><div class=""> func insert(_ value: Value) {</div><div class=""> self[AnyHashable(value)] == value</div><div class=""> }</div><div class="">}<br class=""><blockquote type="cite" class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">item.insert(Foo())<br class="">item.insert(Bar())</span></font></blockquote><div class=""><br class=""></div>So you get at the stored value.<br class=""><br class=""><div class="">-- Howard.</div><div class=""><br class="">On 11 Jul 2017, at 8:09 pm, Glen Huang via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><span class="">Hi, </span><br class=""><span class=""></span><br class=""><span class="">I want to store some heterogeneous items all conform to a protocol inside a set, is it something possible to do in swift?</span><br class=""><span class=""></span><br class=""><span class="">I tried this example:</span><br class=""><span class=""></span><br class=""><span class="">```</span><br class=""><span class="">protocol Named: Hashable {</span><br class=""><span class=""> var name: String { get }</span><br class=""><span class="">}</span><br class=""><span class=""></span><br class=""><span class="">extension Named {</span><br class=""><span class=""> var hashValue: Int {</span><br class=""><span class=""> return name.hashValue</span><br class=""><span class=""> }</span><br class=""><span class=""></span><br class=""><span class=""> static func ==(lhs: Self, rhs: Self) -> Bool {</span><br class=""><span class=""> return lhs.name == rhs.name</span><br class=""><span class=""> }</span><br class=""><span class="">}</span><br class=""><span class=""></span><br class=""><span class="">struct Foo: Named {</span><br class=""><span class=""> var name = "foo"</span><br class=""><span class="">}</span><br class=""><span class=""></span><br class=""><span class="">struct Bar: Named {</span><br class=""><span class=""> var name = "bar"</span><br class=""><span class="">}</span><br class=""><span class=""></span><br class=""><span class="">var item = Set<Named>()</span><br class=""><span class="">item.insert(Foo())</span><br class=""><span class="">item.insert(Bar())</span><br class=""><span class="">```</span><br class=""><span class=""></span><br class=""><span class="">But it failed at `Set<Named>()` where it complained "Using 'Named' as a concrete type conforming to protocol 'Hashable' is not supported”.</span><br class=""><span class=""></span><br class=""><span class="">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?</span><br class=""><span class=""></span><br class=""><span class="">My use case is this:</span><br class=""><span class=""></span><br class=""><span class="">I have an object that can contain two sets of other objects:</span><br class=""><span class=""></span><br class=""><span class="">```</span><br class=""><span class="">class Parent {</span><br class=""><span class=""> var foos: Set<Foo></span><br class=""><span class=""> var bars: Set<Bar></span><br class=""><span class="">}</span><br class=""><span class="">```</span><br class=""><span class=""></span><br class=""><span class="">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?</span><br class=""><span class=""></span><br class=""><span class="">Thanks.</span><br class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-users mailing list</span><br class=""><span class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br class=""></div></blockquote></div></div></div></blockquote></div><br class=""></div></body></html>