<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="">On second thought, this doesn’t seem to solve the problem. Because once I extract the value out, they’re of different types. I can’t uniformly treat them as Named and process them.<div class=""><br class=""></div><div class="">Unless I got the enum definition wrong.</div><div class=""><br class=""></div><div class="">Sorry for the quick-fire mails.</div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 11 Jul 2017, at 9:15 PM, Glen Huang <<a href="mailto:heyhgl@gmail.com" class="">heyhgl@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 style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">NM, I think you meant this?<div class=""><br class=""></div><div class="">enum Either<T1, T2> { <br class=""> case Left(T1) </div><div class=""> case Right(T2)<br class="">}<br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On 11 Jul 2017, at 9:06 PM, Glen Huang via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">This sounds pretty interesting.<div class=""><br class=""></div><div class="">But I can’t totally wrap my head around it. How do I "wrap types into enum cases”? Could you provide a sample code? Thanks.<div class=""><br class=""><blockquote type="cite" class=""><div class="">On 11 Jul 2017, at 8:50 PM, Adrian Zubarev <<a href="mailto:adrian.zubarev@devandartist.com" class="">adrian.zubarev@devandartist.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">If the solution you seek is not designed so that the module user can extend the set of types then you could wrap your types into enum cases and use the enum for your set. ;) When Swift will support anonymous enum cases, this will be an elegant solution to these type of things.<div class=""> <br class=""><div class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px" class="">-- <br class="">Adrian Zubarev<br class="">Sent with Airmail</div></div><p class="gmail_quote" style="">Am 11. Juli 2017 um 14:46:12, Glen Huang via swift-users (<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>) schrieb:</p> <blockquote type="cite" class="gmail_quote"><span class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""></div><div class="">
<title class=""></title>
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 class=""><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="">
<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>
_______________________________________________<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" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></div></span></blockquote>
</div></div></div></blockquote></div><br class=""></div></div>_______________________________________________<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" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></body></html>