<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">`<span style="font-size:12.800000190734863px;font-family:arial,sans-serif">protocol A: GenericProtocol where Instance == String { }` means, A is something that `</span><span style="font-size:12.800000190734863px;font-family:arial,sans-serif">Instance` must be `String`.</span></div><div class="gmail_default"><span style="font-size:12.800000190734863px">However, it doesn't mean `Instance` has already been `String`. </span></div><div class="gmail_default"><span style="font-size:12.800000190734863px"><br></span></div><div class="gmail_default"><span style="font-size:12.800000190734863px">So without assigning your `Instance`, the compiler doesn't know the type of your `Instance`.</span></div><div class="gmail_default"><span style="font-size:12.800000190734863px"><br></span></div><div class="gmail_default"><span style="font-size:12.800000190734863px">Zhao Xin</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jun 26, 2017 at 11:46 AM, Justin Jia via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I’m trying to implement something like this using Swift 4 after proposal 0142 is implemented:<br>
<br>
```<br>
protocol GenericProtocol {<br>
associatedtype Instance<br>
func foo() -> Instance<br>
func bar() -> Instance<br>
// and more...<br>
}<br>
<br>
protocol A: GenericProtocol where Instance == String { }<br>
protocol B: GenericProtocol where Instance == Int { }<br>
protocol C: GenericProtocol where Instance == Double { }<br>
// and more…<br>
<br>
class Bar {<br>
var a: A // Error: Protocol ‘A' can only be used as a generic constraint because it has Self or associated type requirements<br>
var b: B // Error: Protocol ‘B' can only be used as a generic constraint because it has Self or associated type requirements<br>
var c: C // Error: Protocol ‘C' can only be used as a generic constraint because it has Self or associated type requirements<br>
// and more...<br>
}<br>
```<br>
<br>
However, I’m still getting the `Protocol ‘A' can only be used as a generic constraint because it has Self or associated type requirements` error.<br>
<br>
Instead, the only thing I can do right now is to duplicate my code:<br>
<br>
(Just in case your are wondering, I need this syntax to simplify some code I’m working on <a href="https://github.com/TintPoint/Overlay/tree/swift-4.0" rel="noreferrer" target="_blank">https://github.com/TintPoint/<wbr>Overlay/tree/swift-4.0</a>)<br>
<br>
```<br>
protocol A {<br>
func foo() -> String<br>
func bar() -> String<br>
// and more...<br>
}<br>
<br>
protocol B {<br>
func foo() -> Int<br>
func bar() -> Int<br>
// and more...<br>
}<br>
<br>
protocol B {<br>
func foo() -> Double<br>
func bar() -> Double<br>
// and more...<br>
}<br>
<br>
// and more...<br>
<br>
class Bar {<br>
var a: A // OK<br>
var b: B // OK<br>
var c: C // OK<br>
// and more...<br>
}<br>
```<br>
<br>
Am I doing something wrong here? Is it Swift’s current limitation that can be improved in a future version of Swift? Or it needs a special syntax and a separate proposal?<br>
<br>
Thank you in advance for your help!<br>
<br>
Sincerely,<br>
Justin<br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
</blockquote></div><br></div>