<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">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></body></html>