<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="">Hello fellow Swift enthusiasts.</div>
<div class=""><br class="">
</div>
<div class="">I’m struggling to understand why type inference fails to solve <span style="font-family: Consolas;" class="">Collection</span> s associated types while trying to provide it with a default implementation, via protocol extensions, when an additional
subscript is provided. Here is a minimal example:</div>
<div class=""><br class="">
</div>
<div class="">
<div class=""><font face="Consolas" class="">protocol SearchTree: Collection {</font></div>
<div class=""><font face="Consolas" class=""> subscript(key: Int) -> String? { get }</font></div>
<div class=""><font face="Consolas" class="">}</font></div>
<div class=""><font face="Consolas" class=""><br class="">
</font></div>
<div class=""><font face="Consolas" class="">extension SearchTree {</font></div>
<div class=""><font face="Consolas" class=""> // MARK: Conformance to Sequence</font></div>
<div class=""><font face="Consolas" class=""> func makeIterator() -> AnyIterator<(key: Int, value: String)> {</font></div>
<div class=""><font face="Consolas" class=""> return AnyIterator { nil }</font></div>
<div class=""><font face="Consolas" class=""> }</font></div>
<div class=""><font face="Consolas" class=""><br class="">
</font></div>
<div class=""><font face="Consolas" class=""> // MARK: Conformance to Collection</font></div>
<div class=""><font face="Consolas" class=""> var startIndex: Int { return 0 }</font></div>
<div class=""><font face="Consolas" class=""> var endIndex: Int { return 0 }</font></div>
<div class=""><font face="Consolas" class=""> func index(after i: Int) -> Int { return 0 }</font></div>
<div class=""><font face="Consolas" class=""> subscript(key: Int) -> (key: Int, value: String) { return (0, "") }</font></div>
<div class=""><font face="Consolas" class=""><br class="">
</font></div>
<div class=""><font face="Consolas" class=""> // MARK: Conformance to SearchTree</font></div>
<div class=""><font face="Consolas" class=""> subscript(key: Int) -> String? { return nil }</font></div>
<div class=""><font face="Consolas" class="">}</font></div>
<div class=""><font face="Consolas" class=""><br class="">
</font></div>
<div class=""><font face="Consolas" class="">struct ConformingTree: SearchTree { </font></div>
<div class=""><font face="Consolas" class="">}</font></div>
</div>
<div class=""><br class="">
</div>
<div class="">Swift’s compiler complains that <font face="Consolas" class="">ConformingTree</font> doesn’t conform to <span style="font-family: Consolas;" class="">Collection</span>. But it doesn’t say a word if I either remove the additional subscript `<span style="font-family: Consolas;" class="">(key:
Int) -> String?</span>`, or if I push the declaration of the subscript in <span style="font-family: Consolas;" class="">ConformingTree</span>.</div>
<div class=""><br class="">
</div>
<div class="">I asked this question on StackOverflow (<a href="https://stackoverflow.com/questions/46028205" class="">https://stackoverflow.com/questions/46028205</a>), and was kindly taught that I should specify associated types in the protocol (or in the
extension via type aliases) because the type inference was getting confused determining the type of
<font face="Consolas" class="">Collection.Element</font>, having to deal with two subscripts. What I still don’t understand is why the type inference doesn’t need such explicit additional information when the implementation of
<font face="Consolas" class="">SearchTree</font>’s requirement is placed in the concrete type.</div>
<div class=""><br class="">
</div>
<div class="">Could anyone enlighten me on this?</div>
<div class=""><br class="">
</div>
<div class="">Thanks a lot for your time.</div>
<div class="">Best regards.</div>
<br class="">
<div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
Dimitri Racordon<br class="">
CUI, Université de Genève<br class="">
7, route de Drize, CH-1227 Carouge - Switzerland<br class="">
Phone: +41 22 379 01 24
<div class=""><br class="">
</div>
</div>
<br class="Apple-interchange-newline">
<br class="Apple-interchange-newline">
</div>
<br class="">
</body>
</html>