<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 19, 2016, at 9:38 AM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="white-space:pre-wrap" class="">Ad-hoc enums have been discussed already, at length, and all the weaknesses touched on then still apply now. For instance, since they're ad-hoc, can you pass an instance of type "Int | String" as an argument if the function expects a "String | Int | Float"? Enums don't have duck typing behavior like that; if your ad-hoc type does, then it's not very much like an enum; if it doesn't, it won't feel much like a union type.<br class=""></div></div></blockquote><div><br class=""></div><div>IMO one of the nice things about modeling this as syntactic sugar over the semantics of enums is that the answer to this is clear. Right now, no. In the future we may have value type subtyping, in which case the answer may well be yes.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="white-space:pre-wrap" class=""><br class="">Moreover, an ad-hoc "String | Int" may look like a union type, but until switching over an instance to cast it, you can't invoke any methods common to String and Int. So it really doesn't feel like a union type at all.<br class=""></div></div></blockquote><div><br class=""></div><div>I am less convinced of the value of duck-typing, “protocol-like” unions that allow you to access common members than I am of the value of having lightweight syntax for enums that are really just wrappers around a set of possible types.</div><div><br class=""></div><div>For the sake of discussion, lets say we adopt the “syntactic sugar over enums” approach here. In cases where accessing common members is really important it will still be possible.</div><div><br class=""></div><div>// Remember, `Foo | Bar` is syntactic sugar for a type like `enum FooBar`, but the actual name is anonymous.</div><div>extension Foo | Bar {</div><div> func commonMember() {</div><div> switch self {</div><div> case let foo as Foo: foo.commonMember()</div><div> case let bar as Bar: bar.commonMember()</div><div> }</div><div> }</div><div>}</div><div><br class=""></div><div>Granted, this is boilerplate. But that is maybe a good thing. It will guide people away from abusing this as a duck-typed alternative to real protocols. When you need access to the common members you probably *should* be using a protocol instead.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="white-space:pre-wrap" class=""><br class="">Don't get me wrong--like Brent, I'm not convinced I see a scenario in which union types would help write clearly better code, just code that is more "convenient" in the eyes of the beholder. But ad-hoc enums have had their day on this list, and I'm not sure that re-visiting that discussion is going to be very fruitful.<br class=""></div></div></blockquote><div><br class=""></div><div>There have been several discussions around ad-hoc enums and union types. I don’t recall seeing any of them specifically focused on lightweight syntax layered over enums. I *think* this approach avoids many of the reasons the core team has been opposed unions by not exposing any members directly - all you can do with these “unions” is pattern match to extract the payload.</div><div><br class=""></div><div>I would like to see a discussion of something along these lines happen down the road when the time is right.</div><div><br class=""></div><div>As I noted in reply to Brent, adding the ability to implicitly lift cases would be sufficient for the use cases I am aware of right now. I would be satisfied if we made that change and no other. But it would always feel like something that is crying out for a bit more syntactic sugar (coming up with names for some of these enums would be awkward - `Foo | Bar` is really what we want and `enum FooOrBar` just obscures the intent).</div><br class=""><blockquote type="cite" class=""><div class=""><div style="white-space:pre-wrap" class=""><br class="">In any case, this all seems very, very out of scope for Swift 4.</div></div></blockquote><div><br class=""></div><div>I agree with this, certainly for phase 1 in any case. I don’t want to push a distracting discussion right now. I just wanted to respond to Brent with my thoughts.</div><br class=""><blockquote type="cite" class=""><div class=""><br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Fri, Aug 19, 2016 at 9:07 AM Matthew Johnson via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br class="">
> On Aug 19, 2016, at 1:20 AM, Brent Royal-Gordon via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class="">
><br class="">
>> On Aug 18, 2016, at 2:05 AM, Maximilian Hünenberger via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class="">
>><br class="">
>> While purpose of the types are clear in this case there is not only intersection. I also want to find out the distance between different GeometryTypes and other properties like angels between two lines or a Line and a Plane but this doesn't make sense for a Point and some other GeometryType.<br class="">
><br class="">
> But there are extremely complicated interactions between different pairs of types:<br class="">
><br class="">
> Line.intersection(with: Line) -> Void | Point | Line<br class="">
> Line.intersection(with: Circle) -> Void | Point | (Point, Point)<br class="">
> Line.intersection(with: Polygon) -> [Point | Line]<br class="">
><br class="">
> Circle.intersection(with: Line) -> Void | Point | Line<br class="">
> Circle.intersection(with: Circle) -> Void | Point | (Point, Point) | Circle<br class="">
> Circle.intersection(with: Polygon) -> [Point | (Point, Point)]<br class="">
><br class="">
> Polygon.intersection(with: Line) -> [Point | Line]<br class="">
> Polygon.intersection(with: Circle) -> [Point | (Point, Point)]<br class="">
> Polygon.intersection(with: Polygon) -> [Point | Line] | Polygon<br class="">
><br class="">
> What exactly are you planning to do with a `[Point | Line] | Polygon`? Honestly, your only real option is to test it for specific subtypes and try to use them. But there's already a type for that kind of thing: an enum. Enums are better for this application because they lend more structure and allow you to explain cases with descriptive labels:<br class="">
><br class="">
> enum PossiblyCoincidental<CoincidentalType, IntersectingType> {<br class="">
> case coincidental (CoincidentalType)<br class="">
> case intersecting (IntersectingType)<br class="">
> }<br class="">
><br class="">
> typealias LineIntersection = PossiblyCoincidental<Line, Point><br class="">
><br class="">
> enum CircleIntersection {<br class="">
> case tangent (Point)<br class="">
> case secant (Point, Point)<br class="">
> }<br class="">
><br class="">
> Line.intersection(with: Line) -> LineIntersection?<br class="">
> Line.intersection(with: Circle) -> CircleIntersection?<br class="">
> Line.intersection(with: Polygon) -> [LineIntersection]<br class="">
><br class="">
> Circle.intersection(with: Line) -> CircleIntersection?<br class="">
> Circle.intersection(with: Circle) -> PossiblyCoincidental<Circle, CircleIntersection>?<br class="">
> Circle.intersection(with: Polygon) -> [CircleIntersection]<br class="">
><br class="">
> Polygon.intersection(with: Line) -> [LineIntersection]<br class="">
> Polygon.intersection(with: Circle) -> [CircleIntersection]<br class="">
> Polygon.intersection(with: Polygon) -> PossiblyCoincidental<Polygon, [LineIntersection]><br class="">
><br class="">
> This is more complicated, but it's also a lot clearer about what each return type actually means. The existence of rich type information also helps you add functionality:<br class="">
><br class="">
> protocol IntersectionType {<br class="">
> var isEmpty: Bool { get }<br class="">
> }<br class="">
><br class="">
> extension CircleIntersection: IntersectionType {<br class="">
> var isEmpty: Bool { return false }<br class="">
> }<br class="">
><br class="">
> extension PossiblyCoincidental: IntersectionType {<br class="">
> var isEmpty: Bool: { return false }<br class="">
> }<br class="">
><br class="">
> // Cascade inward if IntersectingType happens to itself be an intersection.<br class="">
> extension PossiblyCoincidental where IntersectingType: IntersectionType {<br class="">
> var isEmpty: Bool {<br class="">
> switch self {<br class="">
> case .coincidental:<br class="">
> return false<br class="">
> case .intersecting(let intersection):<br class="">
> return intersection.isEmpty<br class="">
> }<br class="">
> }<br class="">
> }<br class="">
><br class="">
> // Note: Using future conditional conformances<br class="">
> extension Optional: IntersectionType where Wrapped: IntersectionType {<br class="">
> var isEmpty: Bool {<br class="">
> return map { $0.isEmpty } ?? true<br class="">
> }<br class="">
> }<br class="">
><br class="">
> // Retroactive modeling yay!<br class="">
> extension Array: IntersectionType where Element: IntersectionType {}<br class="">
><br class="">
> Of course, it might be the case that this is *way* more information than you really need, and you just want to say:<br class="">
><br class="">
> GeometricElement.intersection(with: GeometricElement) -> [GeometricElement]<br class="">
><br class="">
> But if you want something simple and uniform, you probably don't want the complex `Void | Point | Line`-type stuff, either. Union types are neither simple and uniform, nor complex and descriptive; they are neither fish nor fowl. I just don't see a strong reason to prefer them here.<br class="">
><br class="">
> * * *<br class="">
><br class="">
> I'll ask again what I think has been the crux of this argument from the beginning, and what hasn't really been satisfactorily answered in several months of discussions.<br class="">
><br class="">
> **What use cases are better served by union types than by the alternatives?**<br class="">
><br class="">
> Can you show us code where union types are clearly better—not just shorter—than an equivalent design based on (depending on the need) protocols, enums, or overloading? What about with minor extensions to these features, like closed protocols (allowing for exhaustive checking of protocol types) or implicit type lifting for enums (allowing you to avoid explicitly constructing the case you need, as Optional.some does)?<br class="">
<br class="">
I think implicit lifting with some additional syntactic sugar would be a very good solution here.<br class="">
<br class="">
First, an example of an enum with implicit lifting (using the strawman `autolift` modifier which conservatively could be restricted to cases with a single associated value and could not be used on cases with associated values whose type is also the type of an associated value in another case):<br class="">
<br class="">
enum Foo {<br class="">
autolift case .string(String)<br class="">
autolift case .int(Int)<br class="">
// etc<br class="">
}<br class="">
<br class="">
func bar(_ foos: [Foo]) {}<br class="">
<br class="">
// “a string” is implicitly lifted to be .string(“a string”) and 42 is implicitly lifted to be .int(42)<br class="">
bar([“a string”, 42])<br class="">
<br class="">
This would solve the use case I have run into where I was working on a library design and wanted to accept a heterogeneous collection without requiring callers to have any knowledge beyond what types can be uses (i.e. I don’t want them to have to deal with the syntactic noise of manual lifting, but more importantly I would consider an enum like this an implementation detail, not something users should really rely on).<br class="">
<br class="">
If we *are* going to support implicit lifting like this, why not also adopt syntactic sugar for creating / referencing them in an ad-hoc manner? Rather than writing out the enum above, we could just declare:<br class="">
<br class="">
func bar(_ foos: [String | Int]) {}<br class="">
<br class="">
The enum would be created “on demand” each time a distinct set of types was referenced, with implicit lifting for each case. What is the downside of this syntactic sugar? It would bring the “lightweight” feel of union types to Swift while retaining the semantics of enums. This feels like a nice middle ground to me.<br class="">
<br class="">
As you mention, closed protocols are another possible solution to the design problem I was working on, although it is somewhat less elegant as it would add conformances to the underlying types which is not necessarily desirable.<br class="">
<br class="">
><br class="">
> There is clearly overlap between union types and some of our other features. When do you expect people would use each feature? Are there other languages which support both union types and sum types (i.e. Swift `enum`s)? When do they use each of these?<br class="">
><br class="">
> (Incidentally, even if we had union types, I don't think we'd want to change Optional's definition. Nested optionals are a bit confusing at times, but they're important for correctness in many cases, like collections of Optionals. If Optional is a union type, you also lose the opportunity to have operations like `map` and `flatMap` on it.)<br class="">
><br class="">
> --<br class="">
> Brent Royal-Gordon<br class="">
> Architechies<br class="">
><br class="">
> _______________________________________________<br class="">
> swift-evolution mailing list<br class="">
> <a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div>
</div></blockquote></div><br class=""></body></html>