<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">The title is confusing, let me clarify by example:</div><div class=""><br class=""></div><div class="">We have this protocol with a mutating method:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class="">protocol<span style="color: #000000" class=""> P { </span>mutating<span style="color: #000000" class=""> </span>func<span style="color: #000000" class=""> f(</span>_<span style="color: #000000" class=""> x: </span><span style="color: #703daa" class="">Int</span><span style="color: #000000" class="">) -> </span><span style="color: #703daa" class="">Int</span><span style="color: #000000" class=""> }</span></div></div><div class=""><br class=""></div><div class="">And a conforming class (which has to conform with a non-mutating method):</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">class</span> C: <span style="color: #4f8187" class="">P</span> { <span style="color: #ba2da2" class="">func</span> f(<span style="color: #ba2da2" class="">_</span> x: <span style="color: #703daa" class="">Int</span>) -> <span style="color: #703daa" class="">Int</span> { <span style="color: #ba2da2" class="">return</span> x } }</div></div><div class=""><br class=""></div><div class="">An instance of this class can be used with a let constant:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> c = <span style="color: #4f8187" class="">C</span>()</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #4f8187" class="">c</span><span style="color: #000000" class="">.</span><span style="color: #31595d" class="">f</span><span style="color: #000000" class="">(</span><span style="color: #272ad8" class="">1</span><span style="color: #000000" class="">) </span>// OK</div></div><div class=""> </div><div class="">If we make it an existential object conforming to P, the immutability of the method will be erased:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> c: <span style="color: #703daa" class="">AnyObject</span> & <span style="color: #4f8187" class="">P</span> = <span style="color: #4f8187" class="">C</span>()</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(0, 132, 0);" class=""><span style="text-decoration: underline ; color: #4f8187" class="">c</span><span style="color: #000000" class="">.f(</span><span style="color: #272ad8" class="">1</span><span style="color: #000000" class="">) </span>// </span><font color="#ff2600" class="">Cannot use mutating member on immutable value: 'c' is a 'let' constant</font></div></div><div class=""><br class=""></div><div class="">A generic context has the same issue:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(186, 45, 162);" class="">func</span><font face="Menlo" class=""><span style="font-size: 11px;" class=""> f<T: </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">AnyObject</span><font face="Menlo" class=""><span style="font-size: 11px;" class=""> & </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">P</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">>(</span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(186, 45, 162);" class="">_</span><font face="Menlo" class=""><span style="font-size: 11px;" class=""> arg: </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">T</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">)-> </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Int</span><font face="Menlo" class=""><span style="font-size: 11px;" class=""> { </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(186, 45, 162);" class="">return</span> <span style="font-family: Menlo; font-size: 11px; text-decoration: underline;" class="">a</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">rg.f(</span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(39, 42, 216);" class="">1</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">) } </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(0, 132, 0);" class="">// </span><font color="#ff2600" class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">Cannot use mutating member on immutable value: ‘arg' is a 'let' constant</span></font></font></div></div><div class=""><br class=""></div><div class=""><div class=""><b class="">My question: </b></div><div class=""><br class=""></div><div class="">Is it too much work to preserve method non-mutability in in these cases?</div></div><div class=""><br class=""></div><div class=""><div class="">The workaround I am using is this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">protocol</span><span style="color: #000000" class=""> Q: </span><span style="color: #703daa" class="">class, P </span><span style="color: #000000" class="">{ </span><span style="color: #ba2da2" class="">func</span><span style="color: #000000" class=""> f(</span><span style="color: #ba2da2" class="">_</span><span style="color: #000000" class=""> x: </span><span style="color: #703daa" class="">Int</span><span style="color: #000000" class="">) -> </span><span style="color: #703daa" class="">Int</span><span style="color: #000000" class=""> } </span>// 'Refine' it to be non-mutating.</div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class="">extension<span style="color: #000000" class=""> </span><span style="color: #4f8187" class="">C</span><span style="color: #000000" class="">: </span><span style="color: #4f8187" class="">Q</span><span style="color: #000000" class=""> {}</span></div></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(0, 132, 0);" class="">// Now these work:</div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="color: #ba2da2" class="">let</span> c: <span style="color: #4f8187" class="">Q</span> = <span style="color: #4f8187" class="">C</span>()</div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="color: #4f8187" class="">c</span><span style="color: #000000" class="">.</span><span style="color: #31595d" class="">f</span><span style="color: #000000" class="">(</span><span style="color: #272ad8" class="">1</span><span style="color: #000000" class="">) </span>// OK</div><div style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="color: #ba2da2" class="">func</span> f<T: <span style="color: #4f8187" class="">Q</span>>(<span style="color: #ba2da2" class="">_</span> arg: <span style="color: #4f8187" class="">T</span>)-> <span style="color: #703daa" class="">Int</span> { <span style="color: #ba2da2" class="">return</span> arg.<span style="color: #31595d" class="">f</span>(<span style="color: #272ad8" class="">1</span>) } <span style="color: #008400" class="">// OK</span></div></div><div class=""><br class=""></div><div class="">This workaround creates a lot of duplication and is hard to maintain. It is not something that I do often, but when I do, it is pretty annoying. </div><div class=""><br class=""></div><div class=""><b class="">Supplemental questions:</b></div><div class=""><br class=""></div><div class="">Have you guys ever ran into this?</div></div><div class="">Is there already a bug tracking this? </div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>