<html><head><meta http-equiv="Content-Type" content="text/html charset=gb2312"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Union is far better then generic enum/protocol solution.<div class=""><div class=""> * It can extend the original enum and make it powerful.</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">                </span></div><div class=""><span style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">enum</span><span style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""> ResultDataType {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> Music</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> Video</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> File</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">enum</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> FailureType {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> HTTP404</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> HTTP502</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">enum</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> FailureTypev2 {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> HTTP451</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><div style="margin: 0px; line-height: normal; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">typealias</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> Result = (</span><span style="font-variant-ligatures: no-common-ligatures;" class="">ResultDataType |</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">FailureType |</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">FailureTypev2</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">)</span></div><div style="margin: 0px; line-height: normal; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""><br class=""></span></div></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>* It keeps the code clear and does not need developer to announce some unnecessary protocols or enums.</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>like UnionOf3<T,U,V> or ProtocolForABC</div><div class=""><br class=""></div><div class=""><div class=""> * It is easy to wrap original value into an union type.</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">                </span><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">let</span><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> a = </span><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);">A</span><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures;">()</span></div><div class="" style="font-family: 'Fira Code'; font-size: 11px; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="" style="color: rgb(4, 51, 255);"><span class="Apple-tab-span" style="white-space: pre;">        </span>let</span> union: (A|B|C) = </span><span class="" style="color: rgb(52, 149, 175); font-variant-ligatures: no-common-ligatures;">a </span><span class="" style="color: rgb(0, 143, 0);">// Automatically wrap.</span></div></div><div class=""><br class=""></div><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>* Compiler might search their common properties, and methods, then mark them as a member of the union type.</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">                </span><span style="font-family: 'Fira Code'; font-size: 11px;" class="">print(value.someCommonProperty) </span><span class="" style="font-family: 'Fira Code'; font-size: 11px; color: rgb(0, 143, 0);">// Compiler will know their common properties automatically.</span></div></div><div class=""><span class="" style="font-family: 'Fira Code'; font-size: 11px; color: rgb(0, 143, 0);"><br class=""></span></div><div class=""><span class="" style="font-family: 'Fira Code'; font-size: 11px; color: rgb(0, 143, 0);"><br class=""></span></div><div class=""><span class="" style="font-family: 'Fira Code'; font-size: 11px; color: rgb(0, 143, 0);"><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>* Compiler know the union type exactly composed with which types, better than only know which protocol.</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span class="" style="font-family: 'Fira Code'; font-size: 11px; color: rgb(4, 51, 255);">func</span><span class="" style="font-family: 'Fira Code'; font-size: 11px;"> input(value: </span><span class="" style="font-family: 'Fira Code'; font-size: 11px; color: rgb(52, 149, 175);">ProtocolForABC</span><span class="" style="font-family: 'Fira Code'; font-size: 11px;">) {</span></div></span><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><span class=""><div class="" style="margin: 0px; line-height: normal;"><span class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span class="" style="color: rgb(4, 51, 255);">if</span><span class=""> value </span><span class="" style="color: rgb(4, 51, 255);">is</span><span class=""> </span><span class="" style="color: rgb(52, 149, 175);">A</span><span class=""> {</span></div></span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; min-height: 13px;"><span class=""> </span><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><span class=""> <span class="Apple-tab-span" style="white-space:pre">                </span>} </span><span class="" style="color: rgb(4, 51, 255);">else</span><span class=""> </span><span class="" style="color: rgb(4, 51, 255);">if</span><span class=""> value </span><span class="" style="color: rgb(4, 51, 255);">is</span><span class=""> </span><span class="" style="color: rgb(52, 149, 175);">B</span><span class=""> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; min-height: 13px;"><span class=""> </span><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><span class=""> <span class="Apple-tab-span" style="white-space:pre">                </span>} </span><span class="" style="color: rgb(4, 51, 255);">else</span><span class=""> </span><span class="" style="color: rgb(4, 51, 255);">if</span><span class=""> value </span><span class="" style="color: rgb(4, 51, 255);">is</span><span class=""> </span><span class="" style="color: rgb(52, 149, 175);">C</span><span class=""> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; min-height: 13px;"><span class=""> </span><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><span class=""> <span class="Apple-tab-span" style="white-space:pre">                </span>} </span><span class="" style="color: rgb(4, 51, 255);">else</span><span class=""> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(0, 143, 0);"><span class=""> <span class="Apple-tab-span" style="white-space:pre">                </span></span><span class="">// There are other cases? Compiler doesn't know</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><span class=""> <span class="Apple-tab-span" style="white-space:pre">                </span>}</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><span class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div></div><div class=""><br class=""></div><div class=""> * Original types and union types can have a rational relationship between each other. </div><div class=""> Original type is a sub-type of union types contain it.</div><div class=""><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);"><br class=""></span></span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);"><span class="Apple-tab-span" style="white-space:pre">        </span>var</span> fn0: <span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);">A</span>-><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);">Void</span> = {<span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);">print</span>(v0)}</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="" style="color: rgb(4, 51, 255);"><span class="Apple-tab-span" style="white-space:pre">        </span>var</span> fn1<span class="" style="font-variant-ligatures: no-common-ligatures;">: <font color="#3495af" class="">(A|B)</font></span>-><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);">Void</span> = {<span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);">print</span>(v0)}</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><br class=""></div></div><div class="" style="margin: 0px; line-height: normal;"><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space:pre">        </span>fn0 = fn1 </span><span class="" style="font-family: 'Fira Code'; font-size: 11px; color: rgb(0, 143, 0);">// Original Type and Union Type has a sub-typing relationship, OK</span></div><div class="" style="font-family: 'Fira Code'; font-size: 11px; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class="" style="font-family: 'Fira Code'; font-size: 11px; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="" style="color: rgb(4, 51, 255);"><span class="Apple-tab-span" style="white-space:pre">        </span>var</span> fn2: <span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);">(A|B|C)</span>-><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);">Void</span> = {<span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(52, 149, 175);">print</span>($0)}</span></div><div class="" style="font-family: 'Fira Code'; font-size: 11px; margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class="" style="font-family: 'Fira Code'; font-size: 11px; margin: 0px; line-height: normal;"><span class="Apple-tab-span" style="white-space:pre">        </span>fn0 = fn2 <span class="" style="color: rgb(0, 143, 0);">// OK</span></div><div class=""><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';"><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space:pre">        </span>fn1 = fn2 <span class="" style="color: rgb(0, 143, 0);">// OK</span></span></div></span></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""> * It is also easy to compare with value of original type.</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span><span style="font-family: 'Fira Code'; font-size: 11px;" class="">union == </span><span style="color: rgb(52, 149, 175); font-family: 'Fira Code'; font-size: 11px;" class="">a</span><span style="font-family: 'Fira Code'; font-size: 11px;" class=""> </span><span style="color: rgb(0, 143, 0); font-family: 'Fira Code'; font-size: 11px;" class="">// Can be compared, Yes for most cases.</span></div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>* And the most important part, <b class="">It can replace Optional<T>.</b></div><div class=""><b class=""><span class="Apple-tab-span" style="white-space:pre">                </span></b><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">let</span><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> string: String?</span></div><div class=""><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures;"><span class="Apple-tab-span" style="white-space:pre">        </span>is same to </span></div><div class=""><b class=""><span class="Apple-tab-span" style="white-space:pre">                </span></b><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">let</span><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> string: (String | None) instead of </span><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">let</span><span class="" style="font-family: 'Fira Code'; font-size: 11px; font-variant-ligatures: no-common-ligatures;"> string: Optional<String></span></div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span></div><div class=""><br class=""></div><div class="">I really think the union type is a good supplement for Swift. Make the language rational.</div><div class="">And the It is also really good for Reactive Programming. </div><div class=""><br class=""></div><div class="">- Jiannan </div><div class=""><div class=""><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">下面是被转发的邮件:</div><br class="Apple-interchange-newline"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;" class=""><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif; color:rgba(0, 0, 0, 1.0);" class=""><b class="">发件人: </b></span><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif;" class="">Haravikk <<a href="mailto:swift-evolution@haravikk.me" class="">swift-evolution@haravikk.me</a>><br class=""></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;" class=""><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif; color:rgba(0, 0, 0, 1.0);" class=""><b class="">主题: </b></span><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif;" class=""><b class="">回复: [swift-evolution] Union instead of Optional</b><br class=""></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;" class=""><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif; color:rgba(0, 0, 0, 1.0);" class=""><b class="">日期: </b></span><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif;" class="">2016年5月16日 GMT+8 18:35:25<br class=""></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;" class=""><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif; color:rgba(0, 0, 0, 1.0);" class=""><b class="">收件人: </b></span><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif;" class="">Austin Zheng <<a href="mailto:austinzheng@gmail.com" class="">austinzheng@gmail.com</a>><br class=""></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;" class=""><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif; color:rgba(0, 0, 0, 1.0);" class=""><b class="">抄送: </b></span><span style="font-family: -webkit-system-font, Helvetica Neue, Helvetica, sans-serif;" class="">Cao Jiannan <<a href="mailto:frogcjn@163.com" class="">frogcjn@163.com</a>>, Adrian Zubarev via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>><br class=""></span></div><br class=""><div class=""><div class=""><br class=""><blockquote type="cite" class="">On 16 May 2016, at 11:17, Austin Zheng via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class="">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.<br class=""></blockquote><br class="">I think that a case statement or similar syntax will still be needed, and the case names would just be the types themselves. This would work best with support for type-narrowing, for example:<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func someMethod(value:(A|B|C)) {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>switch (value) {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>case .A:<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>value.someMethodForTypeA()<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>case .B:<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>value.someMethodForTypeB()<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>case .C:<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>value.someMethodForTypeC()<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><br class="">A union should really just be though of as a lightweight, restricted form of enum that can be declared in a quick ad-hoc fashion, similar to how tuples are a simpler form of struct.<br class=""><br class="">I’m generally a +1 for the feature, but I’d be interested to hear about how well equipped the compiler is for optimising something like this. In most cases an Optional covers what I need, and in more complex cases I’d probably declare overloads for each type (i.e- someMethod(value:A), someMethod(value:B) etc.); unions could make the latter case simpler, but will the compiler produce the same code behind the scenes, i.e- by isolating what’s unique to each type?</div></div></blockquote></div><br class=""></div></div></div></div></body></html>