<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 Jun 27, 2017, at 12:39 PM, Jaden Geller via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I’ve run into this issue many times in the real world as well. For example, consider the following protocol:<div class=""><br class=""></div><div class=""><div style="margin: 0px 0px 0px 34px; text-indent: -34px; font-size: 14px; line-height: normal; font-family: Monaco;" class=""><span style="color: #c900a4" class="">protocol</span> OptionalType {</div><div style="margin: 0px 0px 0px 67.9px; text-indent: -68px; font-size: 14px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; associatedtype Wrapped</div><div style="margin: 0px 0px 0px 34px; text-indent: -34px; font-size: 14px; line-height: normal; font-family: Monaco;" class="">}</div></div><div class=""><br class=""></div><div class="">It is not possible to conform `Optional` to this protocol because its generic type is already named `Wrapped`. Only when the associated type can be inferred is conformance possible.</div><div class=""><br class=""></div><div class="">I definitely think we need a solution, but I don’t know what that solution should be.</div></div></div></blockquote><div><br class=""></div><div>I agree. &nbsp;I have run into this as well and have been frustrated by it. &nbsp;It isn’t clear to me what the best solution is but I’d love to see one that could make it into a 4.x release.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Jaden Geller<br class=""><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jun 23, 2017, at 3:52 PM, Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">There could be source-breaking implications for such a feature, especially with retroactive conformance. Therefore, I think this could be very tricky and I'd want to be convinced that the benefits are very great to risk such a disturbance. Here, I think the problem is rather mild, and here's why:<br class=""><br class="">It is true that, in your example specifically, renaming T to U is the only solution (that I know of, anyway). However, for any "serious" protocol P, there's likely to be a required property of type P.T, or a function that takes an argument of type P.T or returns a value of type P.T. Therefore, implementing that requirement in Bar with a corresponding property/argument/return value of type Bar.T would generally do the trick.<br class=""><br class="">Have you got any real-world examples where you're running into this issue?<br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Fri, Jun 23, 2017 at 17:03 David Moore via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Swift Evolution,<br class="">
<br class="">
This may have already been discussed before, but I just came across a bothersome language aspect which reminded me to propose a solution. Currently, if we want to add generics to a protocol the only way to do so is with associated types. I am quite fine with the current approach with respect to those semantics.<br class="">
<br class="">
There is, however, a weakness that is built in with using associated types. That weakness is the lack of associated type and generic inference. To be more clear about what I mean, take the following as an example.<br class="">
<br class="">
protocol Foo {<br class="">
&nbsp; &nbsp; associatedtype T<br class="">
}<br class="">
<br class="">
The foregoing protocol is quite basic, but uses an associated type with the name “T.” Giving the associated type that name will illustrate the dilemma encountered later on down the pipeline.<br class="">
<br class="">
struct Bar&lt;T&gt; : Foo {<br class="">
&nbsp; &nbsp; // What am I supposed to do? The name is used for both the generic and the type alias Foo needs for conformance.<br class="">
&nbsp; &nbsp; typealias T = T // Error!<br class="">
}<br class="">
<br class="">
The above illustrates a situation where we want to connect the generic, which is supposedly named appropriately, and the protocol’s associated type. There is no elegant solution for this at the moment. All I could do is the following.<br class="">
<br class="">
struct Bar&lt;U&gt; : Foo {<br class="">
&nbsp; &nbsp; typealias T = U // Not nearly as readable.<br class="">
}<br class="">
<br class="">
Now, there may be a few ways to go about adding support for generic inference. The compiler as it is already does some awesome inference get when it comes to generics, so why not take it a step further? I propose the introduction of a keyword, or anything else that could work, to specify explicitly what a given type alias declaration would do when it comes to inferencing. Requiring a keyword would ensure source compatibility remains intact, and it would also make the code more readable.<br class="">
<br class="">
I don’t know if this would be something that people could find useful, but I surely would. The implicit mapping of an associated type and a given generic by their names, would be a natural development.<br class="">
<br class="">
Let me know if this is just useless, or if could be a potential feature.<br class="">
<br class="">
Thank you,<br class="">
David Moore<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>