<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>Seems like you want union types (like I do!). Ceylon has great support for union and intersection types and they really shine there!</div><div><br></div><div>See <a href="http://ceylon-lang.org/documentation/1.2/tour/types/">http://ceylon-lang.org/documentation/1.2/tour/types/</a></div><div>and the Talk by Gavin King <a href="http://ceylon-lang.org/community/presentations/eight-ceylon-idioms.pdf">http://ceylon-lang.org/community/presentations/eight-ceylon-idioms.pdf</a></div><div><br></div><div>I'd love to see support for this in Swift. </div><div><br></div><div>-Thorsten</div><div><br>Am 07.12.2015 um 10:53 schrieb William Shipley via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8">The problem I’m trying to solve is if I have<div class=""><br class=""></div><div class="">class LineSegmentObject {</div><div class="">...</div><div class="">}</div><div class="">class Wall : LineSegmentObject {</div><div class="">...</div><div class="">}</div><div class="">class Ray : LineSegmentObject {</div><div class="">…</div><div class="">}</div><div class=""><br class=""></div><div class="">And then in my code if I have</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> wallsInBounds: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Set</span><<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Wall</span>> = …</div></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> raysOffWalls: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Set</span><<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Ray</span>> = ...</div><div class=""><br class=""></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""></blockquote> <span style="color: rgb(187, 44, 162);" class="">let</span> unionOfAllTestedLineSegmentObjects = wallsInBounds.union(raysOffWalls)<br class=""><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><br class=""></div></blockquote>it fails to compile. Instead I have to do the very ugly<div class=""><br class=""></div><div class=""> <span style="color: rgb(187, 44, 162);" class="">var</span> unionOfAllTestedLineSegmentObjects: <span style="color: rgb(112, 61, 170);" class="">Set</span><<span style="color: rgb(79, 129, 135);" class="">LineSegmentObject></span> = (raysOffWalls <span style="color: rgb(187, 44, 162);" class="">as</span> <span style="color: rgb(112, 61, 170);" class="">Set</span><<span style="color: rgb(79, 129, 135);" class="">LineSegmentObject</span>>).union(wallsInBounds <span style="color: rgb(187, 44, 162);" class="">as</span> <span style="color: rgb(112, 61, 170);" class="">Set</span><<span style="color: rgb(79, 129, 135);" class="">LineSegmentObject</span>>)</div><div class=""><br class=""></div><div class="">As an aside, this also fails, surprisingly to me:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> unionOfAllTestedLineSegmentObjects = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Set</span><<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">LineSegmentObject</span>>()</div><div style="margin: 0px; line-height: normal;" class=""> unionOfAllTestedLineSegmentObjects.unionInPlace(wallsInBounds)</div></div><div class=""><br class=""></div><div class="">—</div><div class=""><br class=""></div><div class="">So, what if we had an operator called, say, “commonType” that could take two types and return the closest type the both have in common?</div><div class=""><br class=""></div><div class="">Then the union operator for Set (and similar operators in Array and Dictionary) could look something like:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal; color: rgb(112, 61, 170);" class=""><span style="color: rgb(0, 0, 0);" class=""> </span><span style="color: rgb(187, 44, 162);" class="">public</span><span style="color: rgb(0, 0, 0);" class=""> </span><span style="color: rgb(187, 44, 162);" class="">func</span><span style="color: rgb(0, 0, 0);" class=""> union<S : </span>SequenceType<span style="color: rgb(0, 0, 0);" class="">>(sequence: </span>S<span style="color: rgb(0, 0, 0);" class="">) -> </span>Set<span style="color: rgb(0, 0, 0);" class=""><commonType(</span>Element, S.Generator.Element)<span style="color: rgb(0, 0, 0);" class="">></span></div></div><div class=""><span style="color: rgb(0, 0, 0);" class=""><br class=""></span></div><div class="">instead of:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> union<S : </span>SequenceType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>S<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> == </span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(sequence: </span>S<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) -> </span>Set<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">></span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><br class=""></span></div><div class="">[sorry if I got the syntax of defining a generic wrong up there, I’m still new to this)</div><div class=""><br class=""></div><div class="">So we’d automatically get an Set of LineSegments if we union a Set of Rays with a Set of Walls, which is what I’d expect.</div><div class=""><br class=""></div><div class="">—</div><div class=""><br class=""></div><div class="">If this type of thing doesn’t seem strict enough for you, then it could also just be a separate function, like “heterogenousUnion()” instead of “union()”.</div><div class=""><br class=""></div><div class="">Also, I believe this could make it a LOT easier to build heterogenous Dictionaries in code, which I actually can’t do at all in the current Swift. (Eg, creating dictionaries with arbitrary types of keys and values that you might feed to, say, SCNTechnique.) And it’d make dealing with mixed Arrays easier in a similar way.</div><div class=""><br class=""></div><div class="">--</div><div class=""><br class=""></div><div class="">On a scale from “Lattner" to “Lohan,” how bad an idea is this?</div><div class=""><br class=""></div><div class="">-Wil</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=Z0lfE0AvBRKWSDAcltP5-2FwA6tH7CtZqjBw6KQdxzh8UeEAuMESPncyStoaIO7wH-2BPwbkHuBqCosWJaM-2FYMlewXSePTQFNoXne2MLaBasAcZmiPZ-2FLZabhpBJbNZI1l-2BjtZ0d9u9qyR4zt9JcKplYWHFNlKTS6ALT07PX-2BDZpCRlUk5Enk7k6uDRlWacYopHJvC2RqaQfAlJ5qxhxO9sO-2Fig6UijL7vwLHNrnpnjMrPRsbXasEH0HqBMAeHiIimMq" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;">
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>