<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=""><div class="">What you’re trying to do should be equivalent to this:</div><div class=""><br class=""></div><div class="">protocol Toggling : Equatable {</div><div class=""> …</div><div class="">}</div><div class=""><br class=""></div><div class="">It’s a bug that placing the constraint on ‘Self’ does not have the same effect though; do you mind filing a JIRA?</div><div class=""><br class=""></div><div class="">Slava</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 29, 2017, at 12:24 PM, Ray Fix via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div 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=""> <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=""> <span style="color: #c42275" class="">func</span> toggled() -> <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="" class=""> </span>mutating<span style="" class=""> </span>func<span style="" 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> {</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=""> <span style="color: #c42275" class="">func</span> toggled() -> <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=""> <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=""> <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=""> <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=""> }</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=""> <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=""> <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=""> }</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. </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=""> ^</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=""> 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=""> ^</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=""> 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> Toggling {</font></div><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="">static</span> <span style="color: rgb(196, 34, 117);" class="">var</span> all: [<span style="color: rgb(97, 34, 174);" class="">Self</span>] { <span style="color: rgb(196, 34, 117);" class="">get</span> }</font></div><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="">func</span> toggled() -> <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="" class=""> </span>mutating<span style="" class=""> </span>func<span style="" class=""> 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 <span style="color: rgb(97, 34, 174);" class="">Toggling</span> </font><span style="font-family: Menlo; color: rgb(196, 34, 117);" class="">where</span><span style="font-family: Menlo;" class=""> </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=""> </span><span style="font-family: Menlo; color: rgb(97, 34, 174);" class="">Equatable</span><span style="font-family: Menlo;" class=""> </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=""> <span style="color: rgb(196, 34, 117);" class="">func</span> toggled() -> <span style="color: rgb(97, 34, 174);" class="">Self</span> {</font></div><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="">let</span> current = <span style="color: rgb(196, 34, 117);" class="">Self</span>.all.index(of: <span style="color: rgb(196, 34, 117);" class="">self</span>) ?? <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=""> <span style="color: rgb(196, 34, 117);" class="">let</span> next = (current + <span style="color: rgb(4, 53, 255);" class="">1</span>) % <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=""> <span style="color: rgb(196, 34, 117);" class="">return</span> <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=""> }</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=""> <span style="color: rgb(196, 34, 117);" class="">mutating</span> <span style="color: rgb(196, 34, 117);" class="">func</span> toggle() {</font></div><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="">self</span> = toggled()</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="">}</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. 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 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></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>