<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 30, 2016, at 1:41 PM, Dave Abrahams &lt;<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class="">on Fri Sep 30 2016, Douglas Gregor &lt;<a href="http://dgregor-AT-apple.com" class="">dgregor-AT-apple.com</a>&gt; wrote:<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Sep 29, 2016, at 10:16 PM, Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""><br class="">Obviously I'm a huge +1 for this feature, but I have some concerns and<br class="">questions about the particulars.<br class=""></blockquote><br class=""><blockquote type="cite" class=""><br class="">on Wed Sep 28 2016, Joe Groff &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""><blockquote type="cite" class="">Conditional conformances also have a run-time aspect, because a<br class="">dynamic check for a protocol conformance might rely on the evaluation<br class="">of the extra requirements needed to successfully use a conditional<br class="">conformance. For example:<br class=""><br class="">```swift<br class="">protocol P {<br class=""> func doSomething()<br class="">}<br class=""><br class="">struct S: P {<br class=""> func doSomething() { print("S") }<br class="">}<br class=""><br class="">// Array conforms to P if it's element type conforms to P<br class="">extension Array: P where Element: P {<br class=""> func doSomething() {<br class=""> &nbsp;&nbsp;for value in self {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;value.doSomething()<br class=""> &nbsp;&nbsp;}<br class=""> }<br class="">}<br class=""><br class="">// Dynamically query and use conformance to P.<br class="">func doSomethingIfP(_ value: Any) {<br class=""> if let p = value as? P {<br class=""> &nbsp;&nbsp;p.doSomething()<br class=""> } else {<br class=""> &nbsp;&nbsp;print("Not a P")<br class=""> }<br class="">}<br class=""><br class="">doSomethingIfP([S(), S(), S()]) // prints "S" three times<br class="">doSomethingIfP([1, 2, 3]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// prints "Not a P"<br class="">```<br class=""><br class="">The `if-let` in `doSomethingIfP(_:)` dynamically queries whether the<br class="">type stored in `value` conforms to the protocol `P`. In the case of an<br class="">`Array`, that conformance is conditional, which requires another<br class="">dynamic lookup to determine whether the element type conforms to `P`:<br class="">in the first call to `doSomethingIfP(_:)`, the lookup finds the<br class="">conformance of `S` to `P`. In the second case, there is no conformance<br class="">of `Int` to `P`, so the conditional conformance cannot be used. The<br class="">desire for this dynamic behavior motivates some of the design<br class="">decisions in this proposal.<br class=""></blockquote><br class="">Whether a dynamic evaluation is required at this point seems to depend<br class="">on how you represent conformance. &nbsp;Saying “Array conforms conditionally”<br class="">treats Array as a type, but it might be simpler to treat Array as a<br class="">family of concrete types.<br class=""></blockquote><br class="">This is already the case in the runtime; we specialize the metadata for generic types.<br class=""><br class=""><blockquote type="cite" class=""> I always envisined it this way: at the moment<br class="">a module using the concrete type Array&lt;Foo&gt; comes together with an<br class="">extension (either in that module or in another) that makes Array&lt;T&gt;<br class="">conform to Equatable depending on properties of T, that extension is<br class="">evaluated and Array&lt;Foo&gt; is marked as conforming or not.<br class=""></blockquote><br class="">If the protocol, Array, and Foo all come from different modules, this<br class="">process happens at runtime. Yes, the optimizer could specialize the<br class="">conformance at compile time in many common cases, but you can’t see<br class="">every possible specialization without whole-program analysis, so it<br class="">doesn’t change the model.<br class=""><br class=""><blockquote type="cite" class=""> Then asking<br class="">about a type's conformance is always a simple thing. &nbsp;But I suppose<br class="">which approach wins is dependent on many factors, and the other way can<br class="">be thought of as adding lazy evaluation to the basic model, so if this<br class="">adds nothing to your thinking please excuse the static.<br class=""></blockquote><br class="">Without whole-program information, you need to have the runtime<br class="">capability (or disallow the use of this feature in runtime<br class="">queries). Static specialization can be considered an optimization to<br class="">this model.<br class=""></blockquote><br class="">I know it has to happen at runtime in the worst case. &nbsp;I was suggesting<br class="">it could happen at load time rather than as a complication of the<br class="">dynamic cast machinery. &nbsp;In principle all the load-time information can<br class="">be usefully cached on-disk.<br class=""></div></div></blockquote><div><br class=""></div><div>You can’t compute it at load time, either. There might be sane cases, but here’s a fun one involving existential metatypes:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>protocol P {&nbsp;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&nbsp; static func makeArray() -&gt; [Self] { return [self] }</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>extension Int: P { }</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space: pre;">        </span>extension Array: P where Element: P { }</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>func thwartLoadTime(kind: String) {</div><div><span class="Apple-tab-span" style="white-space: pre;">                </span>let meta: P.Type</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>if kind == “Int” { meta = Int.self }</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>else { meta = String.self }</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>let array: [Any] = meta.makeArray() // Sweet, now I have an array of … something.</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>print(array is P) &nbsp; &nbsp; &nbsp; &nbsp; // true or false?</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div class=""><blockquote type="cite" class="">But, yes, you’re right that (e.g.) overloading across<br class="">differently-constrained extensions and protocol extensions means<br class="">dealing with divergent interfaces… and it’s been a problem for type<br class="">checker and humans both.<br class=""></blockquote><br class="">But that's not going away, so what's the point in bringing it up?<br class=""></div></div></blockquote><div><br class=""></div>I don’t want us to introduce new features that make the problem worse, which allowing overlapping conformances would.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><br class="">Doesn't this break, then?<br class=""><br class=""> &nbsp;protocol Equatable { ... }<br class=""> &nbsp;protocol Comparable : Equatable { ... }<br class=""> &nbsp;protocol Hashable : Equatable { ... }<br class=""><br class=""> &nbsp;extension Array : Comparable where T : Comparable {}<br class=""> &nbsp;extension Array : Hashable where T : Hashable {}<br class=""><br class="">I hope not, beause personally, I don't see how breaking the ability to<br class="">do things like that could be acceptable. &nbsp;The protocols such as<br class="">Equatable et al. that form the components of “Regular” types are *going*<br class="">to be inherited by many other protocols, so this isn't just going to<br class="">happen in a nice localized context, either.<br class=""></blockquote><br class="">The above is ill-formed as written, because we can’t associate<br class="">‘Equatable’ with either conformance without excluding the other—and<br class="">putting it in both places would create an overlapping conformance. <br class=""></blockquote><br class="">(like we said in your office) I don't think the key issue is the<br class="">association of conformances here, but where the Equatable *requirement*<br class="">is getting satisfied.<br class=""></div></div></blockquote><div><br class=""></div><div>Yes, that’s true in the user-facing model.</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class=""><blockquote type="cite" class=""><br class="">So, the fix is to explicitly spell out the Equatable conformance:<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>extension Array: Equatable where T: Equatable { } okay! this is the Equatable conformance for Array<br class=""></blockquote><br class="">This isn't a terrible answer.<br class=""></div></div></blockquote><div><br class=""></div><div>I think it’s actually a rather good answer: this implementation of Equatable is more general/more reusable than either the Comparable or Hashable versions.</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">If we could know that no extensions related to the Comparable or<br class="">Hashable conformance were supplying the Equatable requirement, I think<br class="">we could allow it to be omitted, though. &nbsp;I bet you're going to tell me<br class="">we can't know that, but isn't it enough to use the information we have<br class="">statically at the time we see the above extensions?<br class=""></div></div></blockquote><div><br class=""></div><div>The question you would have to answer is, effectively, “is there a single == to satisfy Equatable that works for all of the constrained extensions?” If you have find declarations to satisfy all of the requirements of the protocol in question, such that each of the constrained extensions can use that one set of declarations to satisfy the same protocol, then maybe it’s plausible… but the result won’t necessarily be as general as “extension Array: Equatable where T: Equatable”.&nbsp;</div><div><br class=""></div><div>It’s also another “global” solver, because it needs to satisfy *all* of the requirements of the protocol and *all* of the conditional conformances that imply conformance to that protocol simultaneously to come up with a correct answer. For example, we might have several implementations of == that might work:</div><div><br class=""></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><div>extension RangeReplaceableCollection where Element: Equatable {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>static func ==(lhs: Self, rhs: Self) -&gt; Bool { /*use ==*/ }</div></div><div><div>}</div></div></blockquote><div><div><br class=""></div><div><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div><div>extension RangeReplaceableCollection where Element: Comparable {</div><div><span class="Apple-tab-span" style="white-space: pre;">        </span>static func ==(lhs: Self, rhs: Self) -&gt; Bool { /*use &lt; and &gt; because we can */ }</div></div><div><div>}</div></div></blockquote><div></div></div><div><br class=""></div><div>The second == is the better one for "extension Array : Comparable where T : Comparable" but is inapplicable for "extension Array : Hashable where T : Hashable”, so we have to choose the first…&nbsp;</div><div><br class=""></div><div>That falls apart if the overlapping conditional conformances are in different modules, of course, because if we only see the "extension Array : Comparable where T : Comparable”, we’ll pick the second ‘==‘… and whoever comes up with "extension Array : Hashable where T : Hashable” in another module will get a failure. The explicit solution of requiring "extension Array: Equatable where T: Equatable” addresses this problem by getting the user involved in creating the appropriate generalization.</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div class=""><br class=""><blockquote type="cite" class="">Note that this conformance is less-specialized than both the “Array:<br class="">Comparable” and “Array: Hashable” conformances, which is important:<br class="">one cannot satisfy “Hashable” (or “Comparable”) without satisfying<br class="">“Equatable”, so that Equatable conformance has to exist and has to be<br class="">usable from the Hashable and Comparable conformances.<br class=""></blockquote><br class="">Suppose I had written:<br class=""><br class="">extension Array where T: Equatable {<br class=""> &nbsp;&nbsp;&nbsp;static func ==(x: Array, y: Array) -&gt; Bool { ... }<br class="">}<br class=""><br class="">Why isn't that enough to allow me to avoid writing the Equatable<br class="">conformance? &nbsp;I'm not asking because I think it's important to avoid it<br class="">once I've written all of the above. &nbsp;I'm just trying to probe/understand<br class="">the model.<br class=""></div></div></blockquote><div><br class=""></div><div>If you allowed the above to fix the problem, the answer to the question “when does Array&lt;T&gt;” conform to Equatable?” is still “if T is Hashable or T is Comparable”, which isn’t what we want. We want “it’s Equatable”.</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">However, in cases where there is a reasonable ordering between the two<br class="">constrained extensions (i.e., one is more specialized than the other),<br class="">the less specialized constrained extension should "win" the implied<br class="">conformance. <br class=""></blockquote><br class="">“Less specialized wins” is surprising at the very least! &nbsp;I've only ever<br class="">seen the opposite rule (e.g. in overload resolution). &nbsp;What's the<br class="">rationale for this approach?<br class=""></blockquote><br class="">You need the most-general implementation so that conformance can be<br class="">used by other constrained extensions, the way the ‘Array: Equatable’<br class="">gets used by ‘Array: Hashable’ and ‘Array: Comparable’.<br class=""></blockquote><br class="">Are you sure we don't want the most specialized common ancestor of all<br class="">the constrained extensions?<br class=""></div></div></blockquote><div><br class=""></div>Yes, that’s correct.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">Continuing the example from above:<br class=""><br class="">```swift<br class="">protocol S: R { }<br class=""><br class="">struct X5&lt;T&gt; { }<br class=""><br class="">extension X5: S where T: S { }<br class=""><br class="">// This last extension "wins" the implied conformance to P, because<br class="">// the extension where "T: R" is less specialized than the one<br class="">// where "T: S".<br class="">extension X5: R where T: R { }<br class="">```<br class=""><br class="">Thus, the rule for placing implied conformances is to pick the *least<br class="">specialized* extension that implies the conformance. If there is more<br class="">than one such extension, then either:<br class=""><br class="">1. All such extensions are not constrained extensions (i.e., they have<br class="">no requirements beyond what the type requires), in which case Swift<br class="">can continue to choose arbitrarily among the extensions, <br class=""></blockquote><br class="">Surely that can't be the right long-term design?<br class=""></blockquote><br class="">When all of the extensions are unconstrained, it still doesn’t matter<br class="">to the user where the conformance goes. In truth, this rule extends to<br class="">groups of extensions that have the *same* constraints—it doesn’t<br class="">matter which one we pick.<br class=""></blockquote><br class="">Seems to me it does if they define different semantics! &nbsp;What am I<br class="">missing?<br class=""></div></div></blockquote><div><br class=""></div>If they have the same constraints, they’ll have the same overload-resolution behavior when picking declarations to satisfy requirements.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">or<br class=""><br class="">2. All such extensions are constrained extensions, in which case the<br class="">program is ill-formed due to the ambiguity. The developer can<br class="">explicitly specify conformance to the protocol to disambiguate.<br class=""></blockquote><br class="">I'm really concerned about the understandability of this model. &nbsp;It<br class="">doesn't seem rational or consistent with other choices in the<br class="">language. Now maybe I'm missing something, but in that case I think you<br class="">haven't given a clear/precise enough description of the rules. &nbsp;But the<br class="">next section looks like it might clear things up for me...<br class=""><br class="">...(I'm sorry to say it didn’t)<br class=""></blockquote><br class="">While the exposition can be improved somewhat, placement of implied<br class="">conformances is fairly tricky and low-level. It will surface to the<br class="">user in cases where the user writes something like your<br class="">Comparable/Hashable example:<br class=""><br class=""><blockquote type="cite" class=""> &nbsp;extension Array : Comparable where T : Comparable {}<br class=""> &nbsp;extension Array : Hashable where T : Hashable {}<br class=""></blockquote><br class="">and the compiler needs to complain about the inability to place<br class="">‘Equatable’ in a way that directs the user to the correct answer<br class="">without exposing them to the entire chain of reasoning we’re going<br class="">through. The diagnostic would need some thought:<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>error: ‘Array' requires an explicit conformance to ‘Equatable’<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>due to conflicting implied conformances (from ‘Comparable’ and<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>‘Hashable’)<br class=""></blockquote><br class="">If we're not making this illegal:<br class=""><br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extension Foo : Comparable { ... }<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extension Foo : Hashable { ... }<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// extension Foo : Equatable { ... }<br class=""><br class="">I think it's going to be tough for people to understand why the<br class="">overlapping implied conformances are a problem in the other case.<br class=""></div></div></blockquote><div><br class=""></div><div>Given the negative feedback we got when I tried to require this before, I don’t think it’s wise to require it except in those cases where it is necessary for conditional conformances. And, of course, it would be a source-breaking change to go back and require this for non-conditional conformances.</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div><div><br class=""></div></div><br class=""></body></html>