<div dir="ltr">A problem with my solution is that there is a conflict between associatedtype declarations inherited from From<Int> and From<Double>.<div>Or in your example, associatedtype Element = Int and associatedtype Element = String are in conflict.</div><div>Another example:</div><div><br></div><div>protocol Computer {</div><div> associatedtype Internal</div><div> mutable func prepare() -> Internal</div><div> mutable func compute(input: Internal)</div><div>}</div><div><br></div><div>extension MyType : Computer<Int> { }</div><div>extension MyType : Computer<Double> { }</div><div><br></div><div>func test<T: Computer>(input: inout T) {</div><div> let internal: T.Internal = input.prepare()</div><div> input.compute(internal)</div><div>}</div><div><br></div><div>What is T.Internal , Int or Double? I showed the problem very eplicitly, but it would exist hidden in a much greater number of cases.</div><div><br></div><div>It's not that such resolution is impossible, but solution of Chris does not have this problem at all: generic types do not create associated type requirements.</div><div>In this case, there is no ambiguity:</div><div><br></div><div><div>protocol Computer<Internal> {</div><div> mutable func prepare() -> Internal</div><div> mutable func compute(input: Internal)</div><div>}</div><div><br></div><div>extension MyType : Computer<Int> { }</div><div>extension MyType : Computer<Double> { }</div><div><br></div><div>func test<I, T: Computer<I>>(input: inout T) {</div><div> let internal: I = input.prepare()</div><div> input.compute(internal)</div><div>}</div></div><div><br></div><div>test(MyType() as Computer<Int>) // no ambiguity</div>test(MyType() as Computer<Double>) // no ambiguity<div><br></div><div>- Anton</div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-06-09 17:25 GMT+03:00 Vladimir.S <span dir="ltr"><<a href="mailto:svabox@gmail.com" target="_blank">svabox@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I like the idea as associatedtype is playing the role of generic type and in extension we conforms to the protocol with some specific generic type as associated type.<br>
<br>
I mean the first idea probably could be<br>
protocol From<T> {<br>
init(_ value: T)<br>
}<br>
but "protocols do not allow generic parameters; use associated types instead", so it seems natural to express concrete type as associated type for protocol in generic syntax <Type><br>
<br>
Probably alternative syntax could look like:<br>
<br>
extension Int : From where .FromType = Float { }<br>
<br>
Also, it seems like this proposal could help to solve a problem with the same name of associated type in different protocols:<br>
<br>
protocol One {<br>
associatedtype Element<br>
func foo(t: Element)<br>
}<br>
<br>
protocol Two {<br>
associatedtype Element<br>
func bar(t: Element)<br>
}<br>
<br>
struct OneTwo : One, Two {<br>
func foo(t: Int) {}<br>
func bar(t: String) {}<br>
}<br>
// type 'OneTwo' does not conform to protocol 'Two'<br>
// candidate has non-matching type '(t: String) -> ()' [with Element = Element]<br>
<br>
So, as I understand, will be possible<br>
struct OneTwo : One, Two<String> {<br>
func foo(t: Int) {} // OneTwo.Element will be Int<br>
func bar(t: String) {}<div><div class="h5"><br>
}<br>
<br>
On 08.06.2016 22:07, Антон Жилин via swift-evolution wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
==Motivation==<br>
<br>
protocol From {<br>
associatedtype FromType<br>
init(_ value: FromType)<br>
}<br>
<br>
The problem is, one type cannot implement multiple From "conversions".<br>
<br>
==Proposed solution==<br>
<br>
Allow specifying all associated types using generic syntax.<br>
<br>
extension Int : From<Float> { }<br>
extension Int : From<Double> { }<br>
<br>
This is only allowed in conformance declarations.<br>
<br>
==Future directions==<br>
<br>
We can replace all *Convertible protocols with From and Into, which will be<br>
defined similarly to Rust.<br>
<br>
- Anton<br>
<br>
<br></div></div><span class="">
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
</span></blockquote>
</blockquote></div><br></div>