<div dir="ltr">I think Union is a good solution as it's opt-in. The reason I'd want opt-in is because the compiler could infer the data size at compile time and possibly eliminate dynamic dispatch if the exact type is known. You'd probably want a shorthand for the union though, for example:<div> MyClass.typeUnion<br><br>The same goes for tuples. At the moment a tuple is the closest thing to a c style struct. I think c structs are currently imported as tuples. If you made it so (A,A) was interchangeable with (B,B) then they'd have to have the same size, and maybe also store type information where they may not otherwise.<br><br>On Tuesday, 16 February 2016, Cao Jiannan via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> 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">Also:<div><br></div><div>union(A,B,C) is subtype of union(A,B,C,D,…)</div><div><br><div><blockquote type="cite"><div>在 2016年2月16日,14:36,Cao Jiannan <<a>frogcjn@163.com</a>> 写道:</div><br><div><div style="word-wrap:break-word"><div>Hi all,</div><div><br></div><div>I think the best way to solve the either problem is to separate it from generic. </div><div>Optional and Either shouldn’t work the same way of generic type. </div><div>It’s just a represent of multiple type in one location.</div><div><br></div><div>Using an old friend, Union in C.</div><div>union {</div><div><span style="white-space:pre-wrap">        </span>case firstType</div><div><span style="white-space:pre-wrap">        </span>case secondType</div><div>}</div><div><br></div><div>This is the final solution for the sub typing problem of optional.</div><div><br></div><div><br></div><div>A == union(A,A)</div><div><div>union(A,B) == union(B,A)</div></div><div>B == union(B,B)</div><div><br></div><div>B is subtype of union(A,B)</div><div>A is subtype of union(A,B)</div><div><br></div><div><div>suppose </div><div>a is subclass of A</div><div>b is subclass of B, then</div></div><div><div><span style="white-space:pre-wrap">        </span>union(a,B) is subtype of union(A,B)</div><div><span style="white-space:pre-wrap">        </span>union(A,b) is subtype of union(A,B)</div></div><div><div><span style="white-space:pre-wrap">        </span>union(a,b) is subtype of union(a,B)</div></div><div><div><span style="white-space:pre-wrap">        </span>union(a,b) is subtype of union(A,b)</div></div><div><br></div><div>union can have as many case as possible. e.g., union(A,B,C,D,…)</div><div><br></div><div>So the Optional<UITableView> should be union(UITableView, None)</div><div>and Optional<MyTableVIew> should be union(MyTableView, None), which is subclass of union(UITableView, None)</div><div><br></div><div>This is a final rational solution. I think.</div><div><br></div><div>-Jiannan</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"><b>发件人: </b></span><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif">Cao Jiannan via swift-evolution <<a>swift-evolution@swift.org</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"><b>主题: </b></span><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif"><b>[swift-evolution] Swift Generic Subtype Problem</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"><b>日期: </b></span><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif">2016年2月16日 GMT+8 11:48:18<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"><b>收件人: </b></span><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif"><a>swift-evolution@swift.org</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"><b>回复-收件人: </b></span><span style="font-family:-webkit-system-font,Helvetica Neue,Helvetica,sans-serif">Cao Jiannan <<a>frogcjn@163.com</a>><br></span></div><br><div><div style="word-wrap:break-word"><div><span style="color:rgb(51,51,51);font-family:inherit;font-size:10pt;font-style:inherit"><br></span></div><span style="color:rgb(51,51,51);font-family:inherit;font-size:10pt;font-style:inherit">Hi all,</span><br><div><div><div style="word-wrap:break-word"><div><span style="font-family:inherit;color:rgb(51,51,51);font-size:10pt;font-style:inherit">I </span><font color="#333333" face="Helvetica Neue" size="2" style="font-family:'Helvetica Neue'">want to discuss on a </font><span style="color:rgb(51,51,51);font-family:'Helvetica Neue';font-size:small">problem</span> about <span style="font-family:'Helvetica Neue';color:rgb(51,51,51);font-size:small">optional generic sub-typing.</span></div><div><div><br></div><div><div>This is my suggesion.</div><div><br></div></div><div style="word-wrap:break-word"><div><div><div style="word-wrap:break-word"><div>if B is subclass of A</div><div>Either<B,B> is subclass of Either<A,A>, Either<A,B>, Either<B,A></div><div>Either<B,A> is subclass of Either<A,A></div><div><div>Either<A,B> is subclass of Either<A,A></div><div><br></div></div></div></div></div></div><blockquote type="cite"><div style="word-wrap:break-word"><div><div><div style="word-wrap:break-word"><div></div><div></div></div></div></div></div></blockquote></div><div>Why? Let’s see an example code in a real project:</div><div><br></div><div><div><div style="word-wrap:break-word"><div style="margin:0px 0px 0px 6px;line-height:normal"><br></div><div style="margin:0px 0px 0px 6px;line-height:normal;font-family:'Helvetica Neue'"><span style="color:rgb(51,51,51);font-family:inherit;font-size:10pt;font-style:inherit"><br></span></div><div style="margin:0px 0px 0px 6px;line-height:normal;font-family:'Helvetica Neue'"><span style="color:rgb(51,51,51);font-family:inherit;font-size:10pt;font-style:inherit">Here is a protocol type for some UIViewController,</span></div><div><div><div style="word-wrap:break-word"><div><div><div style="word-wrap:break-word"><div><div><div style="word-wrap:break-word"><div><div style="padding:1px 0px 0px;border:none;font-family:Consolas,'Courier New',Courier,mono,serif;vertical-align:baseline;background-color:rgb(249,249,249);width:auto;overflow:visible;color:rgb(51,51,51);margin:0px!important"><div style="margin:0px;padding:0px 0px 0px 45px;border:0px;font-style:inherit;font-family:inherit;vertical-align:baseline"><div style="margin:0px;padding:0px;border:0px;font-style:inherit;font-family:inherit;vertical-align:baseline"></div></div><ol start="1" style="padding:0px;border:none;font-style:inherit;font-family:inherit;vertical-align:baseline;list-style-position:initial;background-color:rgb(255,255,255);color:rgb(92,92,92);margin:0px 0px 1px 45px!important"><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;color:inherit;min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important">protocol SegueHandlerType { </span></li><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;background-color:rgb(248,248,248);min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important"> var tableView: UITableView! { get } </span></li><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;color:inherit;min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important">} </span></li></ol></div><div style="margin:0px;padding:0px;border:0px;font-size:16px;font-family:'Myriad Set Pro','Lucida Grande','Helvetica Neue',Helvetica,Arial,Verdana,sans-serif;vertical-align:baseline;line-height:1.4737em;color:rgb(51,51,51)"><br></div><div style="margin:0px;padding:0px;border:0px;font-size:16px;font-family:'Myriad Set Pro','Lucida Grande','Helvetica Neue',Helvetica,Arial,Verdana,sans-serif;vertical-align:baseline;line-height:1.4737em;color:rgb(51,51,51)"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-size:10pt;font-family:inherit;vertical-align:baseline">so the UITableViewController can conform to the protocal</span></div><div style="padding:1px 0px 0px;border:none;font-family:Consolas,'Courier New',Courier,mono,serif;vertical-align:baseline;background-color:rgb(249,249,249);width:auto;overflow:visible;color:rgb(51,51,51);margin:0px!important"><div style="margin:0px;padding:0px 0px 0px 45px;border:0px;font-style:inherit;font-family:inherit;vertical-align:baseline"><div style="margin:0px;padding:0px;border:0px;font-style:inherit;font-family:inherit;vertical-align:baseline"></div></div><ol start="1" style="padding:0px;border:none;font-style:inherit;font-family:inherit;vertical-align:baseline;list-style-position:initial;background-color:rgb(255,255,255);color:rgb(92,92,92);margin:0px 0px 1px 45px!important"><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;color:inherit;min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important">extension UITableViewController : SegueHandlerType { </span></li><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;background-color:rgb(248,248,248);min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important">} </span></li></ol></div><div style="margin:0px;padding:0px;border:0px;font-size:16px;font-family:'Myriad Set Pro','Lucida Grande','Helvetica Neue',Helvetica,Arial,Verdana,sans-serif;vertical-align:baseline;line-height:1.4737em;color:rgb(51,51,51)"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-size:10pt;font-family:inherit;vertical-align:baseline"><br></span></div><div style="margin:0px;padding:0px;border:0px;font-size:16px;font-family:'Myriad Set Pro','Lucida Grande','Helvetica Neue',Helvetica,Arial,Verdana,sans-serif;vertical-align:baseline;line-height:1.4737em;color:rgb(51,51,51)"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-size:10pt;font-family:inherit;vertical-align:baseline">It's great!</span></div><div style="margin:0px;padding:0px;border:0px;font-size:16px;font-family:'Myriad Set Pro','Lucida Grande','Helvetica Neue',Helvetica,Arial,Verdana,sans-serif;vertical-align:baseline;line-height:1.4737em;color:rgb(51,51,51)"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-size:10pt;font-family:inherit;vertical-align:baseline">What if the tableView is a subclass UITableView?</span></div><div style="margin:0px;padding:0px;border:0px;font-size:16px;font-family:'Myriad Set Pro','Lucida Grande','Helvetica Neue',Helvetica,Arial,Verdana,sans-serif;vertical-align:baseline;line-height:1.4737em;color:rgb(51,51,51)"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-size:10pt;font-family:inherit;vertical-align:baseline">like:</span></div><div style="padding:1px 0px 0px;border:none;font-family:Consolas,'Courier New',Courier,mono,serif;vertical-align:baseline;background-color:rgb(249,249,249);width:auto;overflow:visible;color:rgb(51,51,51);margin:0px!important"><div style="margin:0px;padding:0px 0px 0px 45px;border:0px;font-style:inherit;font-family:inherit;vertical-align:baseline"><div style="margin:0px;padding:0px;border:0px;font-style:inherit;font-family:inherit;vertical-align:baseline"></div></div><ol start="1" style="padding:0px;border:none;font-style:inherit;font-family:inherit;vertical-align:baseline;list-style-position:initial;background-color:rgb(255,255,255);color:rgb(92,92,92);margin:0px 0px 1px 45px!important"><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;color:inherit;min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important">class MyTableView : UITableView {</span></li><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;color:inherit;min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important">}</span></li><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;color:inherit;min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important"><br></span></li><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;color:inherit;min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important">MyTableViewController { </span></li><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;background-color:rgb(248,248,248);min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important"> <span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;color:rgb(100,100,100);background-color:inherit;font-size:9pt!important">@IBOutlet</span><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important"> var tableView: MyTableView! </span></span></li><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;background-color:rgb(248,248,248);min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important">} </span></li></ol></div><div style="margin:0px;padding:0px;border:0px;font-size:16px;font-family:'Myriad Set Pro','Lucida Grande','Helvetica Neue',Helvetica,Arial,Verdana,sans-serif;vertical-align:baseline;line-height:1.4737em;color:rgb(51,51,51)"><br></div><div style="margin:0px;padding:0px;border:0px;font-size:16px;font-family:'Myriad Set Pro','Lucida Grande','Helvetica Neue',Helvetica,Arial,Verdana,sans-serif;vertical-align:baseline;line-height:1.4737em;color:rgb(51,51,51)"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-size:10pt;font-family:inherit;vertical-align:baseline">Then </span></div><div style="padding:1px 0px 0px;border:none;font-family:Consolas,'Courier New',Courier,mono,serif;vertical-align:baseline;background-color:rgb(249,249,249);width:auto;overflow:visible;color:rgb(51,51,51);margin:0px!important"><div style="margin:0px;padding:0px 0px 0px 45px;border:0px;font-style:inherit;font-family:inherit;vertical-align:baseline"><div style="margin:0px;padding:0px;border:0px;font-style:inherit;font-family:inherit;vertical-align:baseline"></div></div><ol start="1" style="padding:0px;border:none;font-style:inherit;font-family:inherit;vertical-align:baseline;list-style-position:initial;background-color:rgb(255,255,255);color:rgb(92,92,92);margin:0px 0px 1px 45px!important"><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;color:inherit;min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important">extension MyTableViewController:SegueHandlerType { </span></li><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;background-color:rgb(248,248,248);min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important"> </span></li><li style="border-left-width:3px;border-style:none none none solid;border-left-color:rgb(255,255,255);font-style:inherit;font-size:1em;font-family:inherit;vertical-align:baseline;line-height:12pt;list-style:decimal-leading-zero outside;color:inherit;min-height:12pt;white-space:nowrap;margin:0px!important;padding:0px 3px 0px 10px!important"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;vertical-align:baseline;background-color:inherit;font-size:9pt!important">} </span></li></ol></div><div style="margin:0px;padding:0px;border:0px;font-size:16px;font-family:'Myriad Set Pro','Lucida Grande','Helvetica Neue',Helvetica,Arial,Verdana,sans-serif;line-height:1.4737em"><span style="font-family:Helvetica;font-size:12px">will trigger a compiler error.</span><br style="font-family:Helvetica;font-size:12px"><span style="font-family:Helvetica;font-size:12px"> </span><br style="font-family:Helvetica;font-size:12px"><span style="font-family:Helvetica;font-size:12px">So the Optional needs a subclass system.</span><br style="font-family:Helvetica;font-size:12px"><span style="font-family:Helvetica;font-size:12px">Or to say, that the template system needs a subclass system.</span><br style="font-family:Helvetica;font-size:12px"><span style="font-family:Helvetica;font-size:12px"> </span><br style="font-family:Helvetica;font-size:12px"><span style="font-family:Helvetica;font-size:12px">Optional<UITableView> should be the super type of Optional<MyTableView></span><br style="font-family:Helvetica;font-size:12px"><span style="font-family:Helvetica;font-size:12px">Array<UITableView> should be the super type of Array<MyTableView></span></div></div><div style="margin:0px;padding:0px;border:0px;font-size:16px;font-family:'Myriad Set Pro','Lucida Grande','Helvetica Neue',Helvetica,Arial,Verdana,sans-serif;vertical-align:baseline;line-height:1.4737em;color:rgb(51,51,51)"><br></div><div style="margin:0px;padding:0px;border:0px;vertical-align:baseline;line-height:1.4737em"><font color="#333333" face="Myriad Set Pro, Lucida Grande, Helvetica Neue, Helvetica, Arial, Verdana, sans-serif" size="3"><a href="https://forums.developer.apple.com/message/101646#101646" target="_blank">https://forums.developer.apple.com/message/101646#101646</a></font></div></div></div></div></div></div><blockquote type="cite"><div><div style="word-wrap:break-word"><div><div><div style="word-wrap:break-word"></div></div></div></div></div></blockquote></div><div><br></div><div><div><br></div></div><div><br></div><div>Thanks!</div><div><br></div><div>Jiannan, Cao</div><div><br></div><div><br></div></div></div></div></div></div></div></div></div></div><br></div>_______________________________________________<br>swift-evolution mailing list<br><a>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><br></div></div></blockquote></div><br></div></div></blockquote></div>
</div>