<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">I had a question about defining protocols. Originally I wrote:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class=""><span style="color: #c42275" class="">protocol</span> Toggling <span style="color: #c42275" class="">where</span> <span style="color: #6122ae" class="">Self</span>: <span style="color: #6122ae" class="">Equatable</span> {</font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; <span style="color: #c42275" class="">static</span> <span style="color: #c42275" class="">var</span> all: [<span style="color: #6122ae" class="">Self</span>] { <span style="color: #c42275" class="">get</span> }</font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; <span style="color: #c42275" class="">func</span> toggled() -&gt; <span style="color: #6122ae" class="">Self</span></font></div><div style="margin: 0px; font-size: 18px; line-height: normal; color: rgb(196, 34, 117); background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class=""><span style="color: #000000" class="">&nbsp; </span>mutating<span style="color: #000000" class=""> </span>func<span style="color: #000000" class=""> toggle()</span></font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">}</font></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">extension <span style="color: #6122ae" class="">Toggling</span>&nbsp;{</font></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; <span style="color: #c42275" class="">func</span> toggled() -&gt; <span style="color: #6122ae" class="">Self</span> {</font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; <span style="color: #c42275" class="">let</span> current = <span style="color: #c42275" class="">Self</span>.all.index(of: <span style="color: #c42275" class="">self</span>) ?? <span style="color: #0435ff" class="">0</span></font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; <span style="color: #c42275" class="">let</span> next = (current + <span style="color: #0435ff" class="">1</span>) % <span style="color: #c42275" class="">Self</span>.all.count</font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; <span style="color: #c42275" class="">return</span> <span style="color: #c42275" class="">Self</span>.all[next]</font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; }</font></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; <span style="color: #c42275" class="">mutating</span> <span style="color: #c42275" class="">func</span> toggle() {</font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp; <span style="color: #c42275" class="">self</span> = toggled()</font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; }</font></div><div style="margin: 0px; font-size: 18px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">}</font></div></div><div class=""><br class=""></div><div class="">This resulted in a bunch of errors. &nbsp;</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">Playground execution failed:</b></span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><b class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^</b></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">error: Toggler.playground:7:28: error: cannot invoke 'index' with an argument list of type '(of: Self)'</b></span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">&nbsp; &nbsp; let current = Self.all.index(of: self) ?? 0</b></span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</b></span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""></b></span><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">Toggler.playground:7:28: note: expected an argument list of type '(of: Self.Element)'</b></span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">&nbsp; &nbsp; let current = Self.all.index(of: self) ?? 0</b></span></div></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class=""><br class=""></b></span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><div style="font-family: Helvetica; font-size: 12px;" class="">This approach worked:</div><div style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></div></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class=""><span style="color: rgb(196, 34, 117);" class="">protocol</span>&nbsp;Toggling&nbsp;{</font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp;&nbsp;<span style="color: rgb(196, 34, 117);" class="">static</span>&nbsp;<span style="color: rgb(196, 34, 117);" class="">var</span>&nbsp;all: [<span style="color: rgb(97, 34, 174);" class="">Self</span>] {&nbsp;<span style="color: rgb(196, 34, 117);" class="">get</span>&nbsp;}</font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp;&nbsp;<span style="color: rgb(196, 34, 117);" class="">func</span>&nbsp;toggled() -&gt;&nbsp;<span style="color: rgb(97, 34, 174);" class="">Self</span></font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; color: rgb(196, 34, 117); background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class=""><span style="color: rgb(0, 0, 0);" class="">&nbsp;&nbsp;</span>mutating<span style="color: rgb(0, 0, 0);" class="">&nbsp;</span>func<span style="color: rgb(0, 0, 0);" class="">&nbsp;toggle()</span></font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">}</font></div><div style="font-family: Helvetica; font-size: 12px; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">extension&nbsp;<span style="color: rgb(97, 34, 174);" class="">Toggling</span>&nbsp;</font><span style="font-family: Menlo; color: rgb(196, 34, 117);" class="">where</span><span style="font-family: Menlo;" class="">&nbsp;</span><span style="font-family: Menlo; color: rgb(97, 34, 174);" class="">Self</span><span style="font-family: Menlo;" class="">:</span><span style="font-family: Menlo;" class="">&nbsp;</span><span style="font-family: Menlo; color: rgb(97, 34, 174);" class="">Equatable</span><span style="font-family: Menlo;" class="">&nbsp;</span><span style="font-family: Menlo;" class="">{</span></div><div style="font-family: Helvetica; font-size: 12px; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp;&nbsp;<span style="color: rgb(196, 34, 117);" class="">func</span>&nbsp;toggled() -&gt;&nbsp;<span style="color: rgb(97, 34, 174);" class="">Self</span>&nbsp;{</font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(196, 34, 117);" class="">let</span>&nbsp;current =&nbsp;<span style="color: rgb(196, 34, 117);" class="">Self</span>.all.index(of:&nbsp;<span style="color: rgb(196, 34, 117);" class="">self</span>) ??&nbsp;<span style="color: rgb(4, 53, 255);" class="">0</span></font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(196, 34, 117);" class="">let</span>&nbsp;next = (current +&nbsp;<span style="color: rgb(4, 53, 255);" class="">1</span>) %&nbsp;<span style="color: rgb(196, 34, 117);" class="">Self</span>.all.count</font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(196, 34, 117);" class="">return</span>&nbsp;<span style="color: rgb(196, 34, 117);" class="">Self</span>.all[next]</font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp;&nbsp;}</font></div><div style="font-family: Helvetica; font-size: 12px; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp;&nbsp;<span style="color: rgb(196, 34, 117);" class="">mutating</span>&nbsp;<span style="color: rgb(196, 34, 117);" class="">func</span>&nbsp;toggle() {</font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(196, 34, 117);" class="">self</span>&nbsp;= toggled()</font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">&nbsp;&nbsp;}</font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class="">}</font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><font face="Menlo" class=""><br class=""></font></div><div style="font-family: Helvetica; margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><div style="font-size: 12px;" class="">This version is probably better anyway but I am wondering if the first approach should have shown an error at the point of trying to attach the constraint to the protocol declaration. &nbsp;Any insights on this?</div><div style="font-size: 12px;" class=""><br class=""></div><div style="font-size: 12px;" class="">Thank you,</div><div style="font-size: 12px;" class="">Ray &nbsp;Fix</div><div style="font-size: 12px;" class=""><br class=""></div><div style="font-size: 12px;" class=""><br class=""></div><div class=""><br class=""></div></div></span></div><div class=""><br class=""></div></body></html>