<div dir="ltr">This doesn&#39;t explain how I can use &#39;value&#39; once an A() is passed into the function:<div><br><div><div style="color:rgb(0,0,0);margin:0px;font-size:11px;font-family:&#39;Fira Code&#39;"><span style="color:rgb(4,51,255)">func</span> input(value: (<span style="color:rgb(52,149,175)">A</span> | B | C)) {</div><p style="color:rgb(0,0,0);margin:0px;font-size:11px;font-family:&#39;Fira Code&#39;;min-height:13px">    <br></p><div style="color:rgb(0,0,0);margin:0px;font-size:11px;font-family:&#39;Fira Code&#39;">}</div></div><div><br></div><div>If A, B, and C are not related via protocol or class inheritance, then there is almost nothing you can do with value. Otherwise you still need to test against the concrete type using a case statement or a if-else ladder.</div><div><br></div><div>The other &#39;gain&#39; is being able to call &#39;input(A())&#39;, rather than &#39;input(.caseA(A()))&#39;. I agree that the first form is prettier than the second one. I also think you could make the language pervasively prettier and more expressive by allowing for all sorts of implicit conversions. At some point clarity at the point of use beats conciseness, especially when code within a complex codebase needs to be maintained.</div><div><br></div><div>I understand that this is largely a matter of style - different people value different things, and that&#39;s wonderful. I welcome a formal proposal submitted to the swift-evolution process, and if one appears I&#39;m happy to consider it in more detail and argue for or against it based on that.</div><div><br></div><div>Austin</div><div><br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 15, 2016 at 3:34 AM, Cao Jiannan <span dir="ltr">&lt;<a href="mailto:frogcjn@163.com" target="_blank">frogcjn@163.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">for example, there is a method input union of 3 types: A, B, C,<div><br></div><div>This is the three class.</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">class</span><span> A {}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">class</span><span> B {}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">class</span><span> C {}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span><div style="font-family:Helvetica;font-size:12px">This is how it implemented under Swift 2:</div></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">enum</span><span> UnionABC {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    </span><span style="color:#0433ff">case</span><span> classA(</span><span style="color:#3495af">A</span><span>)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    </span><span style="color:#0433ff">case</span><span> classB(</span><span style="color:#3495af">B</span><span>)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    </span><span style="color:#0433ff">case</span><span> classC(</span><span style="color:#3495af">C</span><span>)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">func</span><span> input(value: </span><span style="color:#3495af">UnionABC</span><span>) {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span>    </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">let</span><span> a = </span><span style="color:#3495af">A</span><span>()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">let</span><span> b = </span><span style="color:#3495af">B</span><span>()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">let</span><span> c = </span><span style="color:#3495af">C</span><span>()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;color:rgb(52,149,175)"><span>input</span><span style="color:#000000">(</span><span>UnionABC</span><span style="color:#000000">.</span><span>classA</span><span style="color:#000000">(</span><span>a</span><span style="color:#000000">))</span></div><div><span style="color:#000000"><br></span></div><div><span style="color:#000000"><br></span></div><div><span style="color:#000000">It needs announce all the cases and name each cases and cannot use class names as their case names.</span></div><div><span style="color:#000000"><br></span></div><div><span style="color:#000000">what about using union? It is more easy and rational.</span></div><div><span style="color:#000000"><br></span></div><div><span style="color:#000000"><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">func</span><span> input(value: (</span><span style="color:#3495af">A</span><span> | B | C)) {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span>    </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">let</span><span> a= </span><span style="color:#3495af">A</span><span>()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;color:rgb(52,149,175)"><span>input</span><span style="color:#000000">(</span><span>a</span><span style="color:#000000">)</span></div><div><span style="color:#000000"><br></span></div></span></div><div><span style="color:#000000">Or you can implement it with protocol and extension, but compiler will not know how many cases it should have.</span></div><div><br></div><div><span style="color:#000000"><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">protocol</span><span> UnionABC {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span>    </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;color:rgb(4,51,255)"><span>extension</span><span style="color:#000000"> </span><span style="color:#3495af">A</span><span style="color:#000000">: </span><span style="color:#3495af">UnionABC</span><span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;color:rgb(4,51,255)"><span>extension</span><span style="color:#000000"> </span><span style="color:#3495af">B</span><span style="color:#000000">: </span><span style="color:#3495af">UnionABC</span><span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;color:rgb(4,51,255)"><span>extension</span><span style="color:#000000"> </span><span style="color:#3495af">C</span><span style="color:#000000">: </span><span style="color:#3495af">UnionABC</span><span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">func</span><span> input(value: </span><span style="color:#3495af">UnionABC</span><span>) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    </span><span style="color:#0433ff">if</span><span> value </span><span style="color:#0433ff">is</span><span> </span><span style="color:#3495af">A</span><span> {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span>        </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    } </span><span style="color:#0433ff">else</span><span> </span><span style="color:#0433ff">if</span><span> value </span><span style="color:#0433ff">is</span><span> </span><span style="color:#3495af">B</span><span> {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span>        </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    } </span><span style="color:#0433ff">else</span><span> </span><span style="color:#0433ff">if</span><span> value </span><span style="color:#0433ff">is</span><span> </span><span style="color:#3495af">C</span><span> {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span>        </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    } </span><span style="color:#0433ff">else</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;color:rgb(0,143,0)"><span style="color:#000000">        </span><span>// There are other cases? Compiler doesn&#39;t know</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:#0433ff">let</span><span> a = </span><span style="color:#3495af">A</span><span>()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;color:rgb(52,149,175)"><span>input</span><span style="color:#000000">(</span><span>a</span><span style="color:#000000">)</span></div><div><span style="color:#000000"><br></span></div><div><span style="color:#000000"><br></span></div></span></div><div><br><blockquote type="cite"><div>下面是被转发的邮件:</div><br><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif;color:rgba(0,0,0,1.0)"><b>发件人: </b></span><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif"><a href="mailto:frogcjn@163.com" target="_blank">frogcjn@163.com</a><br></span></div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif;color:rgba(0,0,0,1.0)"><b>主题: </b></span><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif"><b>回复: [swift-evolution] Union instead of Optional</b><br></span></div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif;color:rgba(0,0,0,1.0)"><b>日期: </b></span><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif">2016年5月15日 GMT+8 18:00:55<br></span></div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif;color:rgba(0,0,0,1.0)"><b>收件人: </b></span><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif">Austin Zheng &lt;<a href="mailto:austinzheng@gmail.com" target="_blank">austinzheng@gmail.com</a>&gt;<br></span></div><br><div><div dir="auto"><span class=""><div><br>Enum and Union are two things.</div><div><br></div><div>If you use Enum to implement Union, you should announce it with case name.</div><div><br></div><div>Another issue using enum instead of union is that,  union can combine types as many as possible, you just write ( A | B | C ... | Z), but for enum, you should carefully announce it for each case. </div><div><br>在 2016年5月15日,15:22,Austin Zheng &lt;<a href="mailto:austinzheng@gmail.com" target="_blank">austinzheng@gmail.com</a>&gt; 写道:<br><br></div></span><blockquote type="cite"><div><span class="">In addition, not everything in Swift can be modeled in terms of inheritance relationships.<div><br></div></span><div><span class="">I&#39;m a little curious as to why union types keep on coming up, when enums can do everything they can and much more (methods, constraints on generic types, conformance to protocols).<br><div><br></div><div>Austin</div></span><div><div><blockquote type="cite"><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite"><div><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><span class=""><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">swift-evolution mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="mailto:swift-evolution@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-evolution@swift.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span></div></blockquote></div><br></div></div>
</div></blockquote></div></div></blockquote></div><br></div></div></blockquote></div><br></div>