<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="">Hi,</div><div class=""><br class=""></div><div class="">Is providing Covariance and Contravariance[1] of generics going to be part of the work on generics for Swift 3? I am sure this topic has come up within the core team and I was wondering what their opinion on the topic was.</div><div class=""><br class=""></div><div class="">I can see this as beneficial as it would allow the compiler - in conjunction with type inference - to retain more type information and hence allow code be more type safe. For example -</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> ConcreteClass<GenType : GenericType> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <font color="#bb2ca2" class="">...</font></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> getFunction() -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">GenType</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <font color="#bb2ca2" class="">...</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> putFunction(input: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">GenType</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> ...</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span> GenericType {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <font color="#bb2ca2" class="">...</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> GenericType1 : <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">GenericType</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <font color="#bb2ca2" class="">...</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> GenericType2 : <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">GenericType</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <font color="#bb2ca2" class="">...</font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> array = [</span>ConcreteClass<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span>GenericType2<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(...</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">), </span>ConcreteClass<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span>GenericType1<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(...</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)]</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">let x : </span>GenericType = <span style="color: rgb(79, 129, 135);" class="">array</span>[<span style="color: rgb(39, 42, 216);" class="">0</span>].getFunction() // this would compile as array would be of type ConcreteClass<types that extend GenericType></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> // currently array is of type AnyObject so this line doesn’t compile</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">array</span>[<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>].putFunction(…) // this would still not compile as it would break type guarantees</div></div><div class=""><br class=""></div><div class="">As a downside I can see it as making generics more complex and difficult to understand. On balance I think probably the benefit in improved type safety is worth it but I was interested in what others thought.</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Simon</div><div class=""><br class=""></div><div class="">[1] <a href="https://dzone.com/articles/covariance-and-contravariance" class="">https://dzone.com/articles/covariance-and-contravariance</a></div></body></html>