<div dir="ltr"><div class="gmail_default" style="font-family:&#39;courier new&#39;,monospace"><span style="font-family:arial,sans-serif">On 7 December 2015 at 10:53, William Shipley via swift-evolution </span><span dir="ltr" style="font-family:arial,sans-serif">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span><span style="font-family:arial,sans-serif"> wrote:</span><br></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">The problem I’m trying to solve is if I have<div><br></div><div>class LineSegmentObject {</div><div>...</div><div>}</div><div>class Wall : LineSegmentObject {</div><div>...</div><div>}</div><div>class Ray : LineSegmentObject {</div><div>…</div><div>}</div><div><br></div><div>And then in my code if I have</div><div><br></div><div><div style="margin:0px;line-height:normal">        <span style="color:rgb(187,44,162)">let</span> wallsInBounds: <span style="color:rgb(112,61,170)">Set</span>&lt;<span style="color:rgb(79,129,135)">Wall</span>&gt; = …</div></div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal">        <span style="color:rgb(187,44,162)">let</span> raysOffWalls: <span style="color:rgb(112,61,170)">Set</span>&lt;<span style="color:rgb(79,129,135)">Ray</span>&gt; = ...</div><div><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"></blockquote>        <span style="color:rgb(187,44,162)">let</span> unionOfAllTestedLineSegmentObjects = wallsInBounds.union(raysOffWalls)<br><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><br></div></blockquote>it fails to compile. Instead I have to do the very ugly<div><br></div><div>        <span style="color:rgb(187,44,162)">var</span> unionOfAllTestedLineSegmentObjects: <span style="color:rgb(112,61,170)">Set</span>&lt;<span style="color:rgb(79,129,135)">LineSegmentObject&gt;</span> = (raysOffWalls <span style="color:rgb(187,44,162)">as</span> <span style="color:rgb(112,61,170)">Set</span>&lt;<span style="color:rgb(79,129,135)">LineSegmentObject</span>&gt;).union(wallsInBounds <span style="color:rgb(187,44,162)">as</span> <span style="color:rgb(112,61,170)">Set</span>&lt;<span style="color:rgb(79,129,135)">LineSegmentObject</span>&gt;)</div><div><br></div><div>As an aside, this also fails, surprisingly to me:</div><div><br></div><div><div style="margin:0px;line-height:normal">        <span style="color:rgb(187,44,162)">var</span> unionOfAllTestedLineSegmentObjects = <span style="color:rgb(112,61,170)">Set</span>&lt;<span style="color:rgb(79,129,135)">LineSegmentObject</span>&gt;()</div><div style="margin:0px;line-height:normal">        unionOfAllTestedLineSegmentObjects.unionInPlace(wallsInBounds)</div></div><div><br></div><div>—</div><div><br></div><div>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><br></div><div>Then the union operator for Set (and similar operators in Array and Dictionary) could look something like:</div><div><br></div><div><div style="margin:0px;line-height:normal;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(187,44,162)">public</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">func</span><span style="color:rgb(0,0,0)"> union&lt;S : </span>SequenceType<span style="color:rgb(0,0,0)">&gt;(sequence: </span>S<span style="color:rgb(0,0,0)">) -&gt; </span>Set<span style="color:rgb(0,0,0)">&lt;commonType(</span>Element, S.Generator.Element)<span style="color:rgb(0,0,0)">&gt;</span></div></div><div><span style="color:rgb(0,0,0)"><br></span></div><div>instead of:</div><div><br></div><div><div style="margin:0px;line-height:normal;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(187,44,162)">public</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">func</span><span style="color:rgb(0,0,0)"> union&lt;S : </span>SequenceType<span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">where</span><span style="color:rgb(0,0,0)"> </span>S<span style="color:rgb(0,0,0)">.</span>Generator<span style="color:rgb(0,0,0)">.</span>Element<span style="color:rgb(0,0,0)"> == </span>Element<span style="color:rgb(0,0,0)">&gt;(sequence: </span>S<span style="color:rgb(0,0,0)">) -&gt; </span>Set<span style="color:rgb(0,0,0)">&lt;</span>Element<span style="color:rgb(0,0,0)">&gt;</span></div></div><div><span style="color:rgb(0,0,0)"><br></span></div><div>[sorry if I got the syntax of defining a generic wrong up there, I’m still new to this)</div><div><br></div><div>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></div></div></blockquote><div><br></div>I think I like the idea, but I don’t think we should need a dedicated operator for this.<br>How do you feel about a declaration like this:<br><br>    func union&lt;S: SequenceType, E where Element: E, S.Generator.Element: E&gt;(sequence: S) -&gt; Set&lt;E&gt;<br><br>Martin</div><div class="gmail_quote"><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div>—<br></div><div>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><br></div><div>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><br></div><div>--</div><div><br></div><div>On a scale from “Lattner&quot; to “Lohan,” how bad an idea is this?</div><div><br></div><div>-Wil</div></div></blockquote></div></div></div>