[swift-evolution] Swift Generic Subtype Problem

Sune Foldager cyano at me.com
Tue Feb 16 12:47:13 CST 2016


This, which is generic sub-typing variance, has already been discussed elsewhere on the list. One problem with what you suggest is that it’s not in general safe to assume that because A : B then X<A> : X<B>. Sometimes it’s the other way around, and often neither applies. For example, it’s not safe for arrays in general (although it might be for Swift arrays due to their value type semantics).

-Sune

> On 16 Feb 2016, at 04:46, Cao Jiannan via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
> Hi all,
> I want to discuss on optional generic sub-typing problem.
> 
> This is my suggesion.
> 
> if B is subclass of A
> Either<B,B> is subclass of Either<A,A>, Either<A,B>, Either<B,A>
> Either<B,A> is subclass of Either<A,A>
> Either<A,B> is subclass of Either<A,A>
> 
> 
> Why? Let’s see an example code in a real project:
> 
> 
> 
> Here is a protocol type for some UIViewController,
> protocol  SegueHandlerType {  
>     var tableView: UITableView! { get }  
> }  
> 
> so the UITableViewController can conform to the protocal
> extension UITableViewController : SegueHandlerType {  
> }  
> 
> It's great!
> What if the tableView is a subclass UITableView?
> like:
> class MyTableView : UITableView {
> }
> 
> MyTableViewController {  
>       @IBOutlet var tableView: MyTableView!  
> }  
> 
> Then 
> extension MyTableViewController:SegueHandlerType {  
>   
> }  
> will trigger a compiler error.
>  
> So the Optional needs a subclass system.
> Or to say, that the template system needs a subclass system.
>  
> Optional<UITableView> should be the super type of Optional<MyTableView>
> Array<UITableView> should be the super type of Array<MyTableView>
> 
> https://forums.developer.apple.com/message/101646#101646 <https://forums.developer.apple.com/message/101646#101646>
> 
> 
> 
> Thanks!
> 
> Jiannan, Cao
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160216/35cd440a/attachment.html>


More information about the swift-evolution mailing list