<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=""><div class=""><font size="2" class="">I can see it is a (counter-intuitive) language design decision for type safety… but then why, in the code below I can:</font></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 18px;" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(97, 34, 174);" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c42275" class="">class</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> OtherThing: </span><span style="font-variant-ligatures: no-common-ligatures; color: #3c828b" class="">Something</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures" class="">UIViewController</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> {</span></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: #c42275" class="">override</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c42275" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> start(</span><span style="font-variant-ligatures: no-common-ligatures; color: #c42275" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> completion: </span><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4" class="">SomeCallback</span><span style="font-variant-ligatures: no-common-ligatures" class="">? = </span><span style="font-variant-ligatures: no-common-ligatures; color: #c42275" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(30, 148, 33);" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-variant-ligatures: no-common-ligatures" class="">// implementation details...</span></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font size="2" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font size="2" class="">}</font></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 18px;" class=""><font size="2" class=""><br class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c42275" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> firstThing = </span><span style="font-variant-ligatures: no-common-ligatures" class="">OtherThing(viewController: </span><span style="font-variant-ligatures: no-common-ligatures; color: #6122ae" class="">UINavigationController</span><span style="font-variant-ligatures: no-common-ligatures" class="">())</span></font></div></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font size="2" class=""><br class=""></font></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">OtherThing extends </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 130, 139);" class="">Something</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class="">UIViewController</span><span style="font-variant-ligatures: no-common-ligatures;" class="">>… but I can instantiate it with the subtype…</span></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font size="2" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font size="2" class="">Ok you will say, UINavigationController is a subtype of UIViewController, but that still does not make <span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 130, 139);" class="">Something</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="color: rgb(97, 34, 174);" class="">UINavigationController</span>> a subtype of </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 130, 139);" class="">Something</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class="">UIViewController</span><span style="font-variant-ligatures: no-common-ligatures;" class="">>. </span></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">Fair enough, but:</span></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font size="2" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal; color: rgb(97, 34, 174);" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c42275" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> c1: </span><span style="font-variant-ligatures: no-common-ligatures; color: #3c828b" class="">Something</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures" class="">UIViewController</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #3c828b" class="">Something</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(viewController: </span><span style="font-variant-ligatures: no-common-ligatures" class="">UINavigationController</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">())</span></font></div><div style="margin: 0px; line-height: normal; color: rgb(97, 34, 174);" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">// c1 is of type "</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 130, 139);" class="">Something</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="font-variant-ligatures: no-common-ligatures;" class="">UIViewController</span><span style="font-variant-ligatures: no-common-ligatures;" class="">>"</span></font></div><div style="margin: 0px; line-height: normal; color: rgb(97, 34, 174);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><font size="2" class=""><br class=""></font></span></div><div style="margin: 0px; line-height: normal;" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c42275" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> c2 = </span><span style="font-variant-ligatures: no-common-ligatures; color: #3c828b" class="">Something</span><span style="font-variant-ligatures: no-common-ligatures" class="">(viewController: </span><span style="font-variant-ligatures: no-common-ligatures; color: #6122ae" class="">UINavigationController</span><span style="font-variant-ligatures: no-common-ligatures" class="">())</span></font></div></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">// c1 is of type "</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 130, 139);" class="">Something</span><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class="">UINavigationController>”</span></font></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class=""><font size="2" class=""><br class=""></font></span></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class="">So it appears </span></span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 130, 139);" class="">Something</span><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class="">UINavigationController> can be cast to type </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(60, 130, 139);" class="">Something</span><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class="">UIViewController</span><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class="">>…</span></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(97, 34, 174); font-variant-ligatures: no-common-ligatures;" class=""><font size="2" class=""><br class=""></font></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font color="#6122ae" size="2" class="">Yet this is illegal?</font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><font color="#6122ae" size="2" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><font size="2" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c42275" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> somethings: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #3c828b" class="">Something</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #6122ae" class="">UIViewController</span><span style="font-variant-ligatures: no-common-ligatures" class="">>] = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4" class="">c1</span><span style="font-variant-ligatures: no-common-ligatures" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #539aa4" class="">c2</span><span style="font-variant-ligatures: no-common-ligatures" class="">]</span></font></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><font size="2" class=""><br class=""></font></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><font size="2" class="">I dont know, something seems inconsistent.</font></span></div><div style="font-size: 15px; margin: 0px; line-height: normal;" class=""><br class=""></div></div><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 16, 2017, at 10:59 PM, Slava Pestov <<a href="mailto:spestov@apple.com" class="">spestov@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi Isaac,<br class=""><br class="">This is not about associated types. Rather, the issue is that a ‘Thing’ is a ‘Something<UINavigationController>’, but you are casting it to ‘Something<UIViewController>’. The two types are not related; in general, if A is a subtype of B, then G<A> is not a subtype of G<B>.<br class=""><br class=""><a href="https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)" class="">https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)</a><br class=""><br class="">Slava<br class=""><br class=""><blockquote type="cite" class="">On Feb 16, 2017, at 9:05 AM, Isaac Rivera via swift-users <swift-users@swift.org> wrote:<br class=""><br class="">Hello, list!<br class=""><br class="">I am trying to find my way around Swift’s protocol limitations. It appears that in general, any protocol with declared associatedtype will break polymorphism?<br class=""><br class="">Take the case below which does not compile. All "Thing” instances are "Something<VC: UIViewController>” but they can’t be passed around or coerced as so.<br class=""><br class="">How is it that I can legally write the code:<br class=""><br class="">class Thing: Something<UINavigationController> { }<br class=""><br class="">and instantiate it, but it is not the very thing it implements? <br class=""><br class="">All Thing instances conform to the public interfaces of Something<UIViewController> so why can’t they be recognized as such and coerced as such?<br class=""><br class="">What is the work-around of this break in Polymorphism?<br class=""><br class="">import UIKit<br class=""><br class="">protocol Anything: class, NSObjectProtocol {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>associatedtype ViewControllerType: UIViewController<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var viewController: ViewControllerType { get }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>init(viewController: ViewControllerType)<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func addAnything(anything: Something<UIViewController>) -> Bool<br class="">}<br class=""><br class="">class Something<VC: UIViewController>: NSObject, Anything {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>typealias ViewControllerType = VC<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>private(set) var viewController: ViewControllerType<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>required init(viewController: ViewControllerType) { self.viewController = viewController }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>final private var things = [String: Something<UIViewController>]()<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>final internal func addAnything(anything: Something<UIViewController>) -> Bool {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>// implementation details...<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>return true<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class="">}<br class=""><br class="">class Thing: Something<UINavigationController> { }<br class=""><br class="">let firstThing = Thing(viewController: UINavigationController())<br class="">let secondThing = Thing(viewController: UINavigationController())<br class=""><br class="">firstThing.addAnything(anything: secondThing)<br class=""><br class="">// Playground execution failed: error: MyPlayground.playground:48:34: error: cannot convert value of type 'Thing' to expected argument type 'Something<UIViewController>'<br class=""><br class="">firstThing.addAnything(anything: secondThing as Something<UIViewController>)<br class=""><br class="">// Playground execution failed: error: MyPlayground.playground:48:34: error: cannot convert value of type 'Thing' to type 'Something<UIViewController>' in coercion<br class=""><br class=""><br class=""><br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class="">swift-users@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></blockquote><br class=""></div></div></blockquote></div><br class=""></body></html>