<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=""><div class="">I like it.</div><div class=""><br class=""></div><div class="">This was actually one of the first problems I ran into when I first tried swift.</div><div class=""><br class=""></div>I would really like to see syntax like ‘TypeA | TypeB’ meaning the type could be either ‘TypeA' or ‘TypeB’. You should be able to call any functions/methods which are common to both types without having to cast.<div class=""><br class=""></div><div class="">As you say, the result of union (or similar collection building methods) should be the intersection of the types together in the collection (and I believe the above idea would give the type system a concrete way to reason about that). So in your case unionOfAllTestedLineSegmentObjects would have type Set<Wall | Ray>. ‘Wall | Ray’ would essentially be the same as LineSegmentObject, but might also have extra methods available (e.g. say they both ended up conforming to a protocol which LineSegmentObject doesn’t).</div><div class=""><br class=""></div><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="" style="margin: 0px; color: rgb(112, 61, 170);"><span class="" style="color: rgb(187, 44, 162);"><span class="Apple-tab-span" style="white-space:pre">        </span>public</span><span class=""> </span><span class="" style="color: rgb(187, 44, 162);">func</span><span class=""> union<S : </span>SequenceType<span class="">>(sequence: </span>S<span class="">) -> </span>Set<span class=""><</span>Element | S.Generator.Element<span class="">></span></div></div></div></div><div class=""><br class=""></div><div class="">I could also explicitly build an array of type [String | Int] where in some cases I care about which one it is (and thus cast with is or as? to find out), and in other cases I don’t, and just call something they have in common (e.g. comparable)</div><div class=""><br class=""></div><div class="">The biggest win would be for things like JSON, where there are an exact number of very different types allowed. Currently we have to use ‘Any’ to represent that mix of types, but with the above idea, we could represent that requirement exactly.</div><div class=""><br class=""></div><div class="">It allows us to have much more fluidity (something closer to duck-typing) while still keeping full type safety.</div><div class=""><br class=""></div><div class="">I do realize that is a large ask though, since it involves the type system...</div><div class=""><br class=""></div><div class=""><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 7, 2015, at 1:53 AM, William Shipley via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@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; -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="" class=""> </span><span style="color: rgb(187, 44, 162);" class="">public</span><span style="" class=""> </span><span style="color: rgb(187, 44, 162);" class="">func</span><span style="" class=""> union<S : </span>SequenceType<span style="" class="">>(sequence: </span>S<span style="" class="">) -> </span>Set<span style="" class=""><commonType(</span>Element, S.Generator.Element)<span style="" class="">></span></div></div><div class=""><span style="" 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="" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="" class=""> union<S : </span>SequenceType<span style="" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span><span style="" class=""> </span>S<span style="" class="">.</span>Generator<span style="" class="">.</span>Element<span style="" class=""> == </span>Element<span style="" class="">>(sequence: </span>S<span style="" class="">) -> </span>Set<span style="" class=""><</span>Element<span style="" class="">></span></div></div><div class=""><span style="" 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=vmG20pDvfvCvG17-2BDXZuJLwefEI-2FHl6Yo9DaSFXQk5lClj3ePG1prMfWHHmdp6Pjbr-2FuSFXzlWshJsTG9EOumblnxeUVBqr5BLMA63moSVvAeqGq7m8JFlaLhBCu63sk128wa0wca3XopO6d65M36-2Bu0aGbeKg97bkNs0RG2ocHM4q3sk23vz-2BTXGNrAYdELOnqEI6DzyCgUmud-2FqURMtA-3D-3D" 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;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></div></body></html>