<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=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jul 12, 2017, at 3:23 AM, Gor Gyolchanyan via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hello, swift community!<div class=""><br class=""></div><div class="">Recently I’ve come across a dilemma regarding value-type semantics when dealing with generic types.</div><div class="">Consider a protocol that has a mutating in-place function and a non-mutating returning variant of that function:</div><div class=""><br class=""></div><div class=""><font face="PT Mono" class=""><font color="#941751" class="">protocol</font> <font color="#008f00" class="">Transmogrifier</font> {</font></div><div class=""><font face="PT Mono" class=""><br class=""></font></div><div class=""><font face="PT Mono" class="">&nbsp; &nbsp; <font color="#941751" class="">mutating</font>&nbsp;<font color="#941751" class="">func</font> transmogrify()</font></div><div class=""><font face="PT Mono" class=""><br class=""></font></div><div class=""><font face="PT Mono" class="">&nbsp; &nbsp;&nbsp;<font color="#941751" class="">func</font>&nbsp;transmogrified() -&gt; <font color="#941751" class="">Self</font></font></div><div class=""><font face="PT Mono" class=""><br class=""></font></div><div class=""><font face="PT Mono" class="">}</font></div><div class=""><br class=""></div><div class="">One of these methods has to have a default implementation in terms of the other.</div><div class=""><br class=""></div><div class="">One way doing it is to implement the mutating version in terms of non-mutating because it doesn’t depend on additional conditions to work, since assigning to `self` causes a complete copy of the internal state of the object regardless of whether it’s a value type or a reference type. However, this approach has a big downside: in many cases mutating functions mutate only part of the instance, which means that an efficient implementation will have to implement the mutating version and because of the way the default implementation works, the non-mutating version would also need to be manually implemented, which makes the default implementation useless in those cases.</div><div class=""><br class=""></div><div class="">Implementing the non-mutating version in terms of mutating version solves this problem nicely, allowing one to focus on mutating only the necessary parts of the instance, while leaving the need to return a separate instance to the default implementation, which would be perfectly adequate in most cases. This approach has its own problem that this pitch seeks to solve. The problem becomes apparent when you consider this naive implementation:</div><div class=""><br class=""></div><div class=""><font face="PT Mono" class=""><font color="#941751" class="">extension</font> <font color="#008f00" class="">Transmogrifier</font> {</font></div><div class=""><font face="PT Mono" class=""><br class=""></font></div><div class=""><font face="PT Mono" class="">&nbsp; &nbsp; <font color="#941751" class="">public</font> <font color="#941751" class="">func</font>&nbsp;transmogrified() -&gt; <font color="#941751" class="">Self</font> {</font></div><div class=""><font face="PT Mono" class="">&nbsp; &nbsp; &nbsp; &nbsp; <font color="#941751" class="">var</font> result = <font color="#941751" class="">self</font></font></div><div class=""><font face="PT Mono" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>result.<font color="#005493" class="">transmogrify</font>()</font></div><div class=""><font face="PT Mono" class=""><span class="Apple-tab-span" style="white-space:pre">        </span><font color="#941751" class="">return</font> result</font></div><div class=""><font face="PT Mono" class="">&nbsp; &nbsp; }</font></div><div class=""><font face="PT Mono" class=""><br class=""></font></div><div class=""><font face="PT Mono" class="">}</font></div><div class=""><br class=""></div><div class="">The above implementation is&nbsp;only&nbsp;correct for value types, because assignment is a deep copy. If the instance is of a reference type, the assignment will do nothing and the call to the mutating version will apply to the original object, violating the postcondition of the function (which states that the function shall not modify the instance in any way).</div><div class=""><br class=""></div><div class="">The most straight-forward way of solving this problem is to introduce a new protocol for making sure the original instance is always copied:</div></div></div></blockquote><div><br class=""></div><div>Immutable types like NSString, NSDictionary etc just return self for the copy.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class=""><font face="PT Mono" class=""><font color="#941751" class="">protocol</font> <font color="#008f00" class="">CopyInitializable</font> {</font></div><div class=""><font face="PT Mono" class=""><br class=""></font></div><div class=""><font face="PT Mono" class="">&nbsp; &nbsp; <font color="#941751" class="">init</font>(copying other: <font color="#941751" class="">Self</font>)</font></div><div class=""><font face="PT Mono" class=""><br class=""></font></div><div class=""><font face="PT Mono" class="">}</font></div><div class=""><br class=""></div><div class="">In which case the default implementation becomes fully correct:</div><div class=""><br class=""></div><div class=""><div class=""><font face="PT Mono" color="#797979" class="">// The `CopyInitializable` conformance can also be moved to the protocol itself</font></div><div class=""><font color="#797979" class=""><font face="PT Mono" class="">// if the&nbsp;</font><span style="font-family: 'PT Mono';" class="">protocol conformance requires value-type semantics.</span></font></div><div class=""></div></div><div class=""><span style="font-family: 'PT Mono';" class=""><font color="#941751" class="">extension</font> <font color="#008f00" class="">Transmogrifier</font> <font color="#941751" class="">where</font> <font color="#941751" class="">Self</font>: <font color="#008f00" class="">CopyInitializable</font> {</span></div><div class=""><div class=""><div class=""><span style="font-family: 'PT Mono';" class="">&nbsp; &nbsp;&nbsp;</span></div></div><div class=""><font face="PT Mono" class="">&nbsp; &nbsp; <font color="#941751" class="">public</font> <font color="#941751" class="">func</font>&nbsp;transmogrified() -&gt; <font color="#941751" class="">Self</font> {</font></div><div class=""><font face="PT Mono" class="">&nbsp; &nbsp; &nbsp; &nbsp; <font color="#941751" class="">var</font> result = <font color="#941751" class="">Self</font>(copying: <font color="#941751" class="">self</font>)</font></div><div class=""><font face="PT Mono" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>result.<font color="#005493" class="">transmogrify</font>()</font></div><div class=""><font face="PT Mono" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><font color="#941751" class="">return</font> result</font></div><div class=""><font face="PT Mono" class="">&nbsp; &nbsp; }</font></div><div class=""><font face="PT Mono" class=""><br class=""></font></div><div class=""><font face="PT Mono" class="">}</font></div></div><div class=""><br class=""></div><div class="">The downside of this approach is the need to manage <font face="PT Mono" color="#008f00" class="">CopyInitializable</font> conformance of the types &nbsp;that becomes extra hassle that seems to conflict with the behavior of value types.</div><div class=""><br class=""></div><div class="">This pitch proposes adding <font face="PT Mono" color="#008f00" class="">CopyInitializable</font> protocol to the swift standard library and having the compiler automatically generate conformance to it for all value types.</div><div class="">This would immediately solve all problems of correct convenient implementations of non-mutaiting variants of in-place functions as well as remove the hassle of having to manage conformance to <font face="PT Mono" color="#008f00" class="">CopyInitializable</font>&nbsp;for all value types that are guaranteed to have this behavior in the first place.</div><div class=""><font face="AvenirNext-Regular" class=""><br class=""></font></div><div class=""><font face="AvenirNext-Regular" class="">An good&nbsp;use case would be the </font><font face="PT Mono" color="#008f00" class="">NSNumber</font><font face="AvenirNext-Regular" class=""> class, which would conform to&nbsp;</font><font face="PT Mono" color="#008f00" class="">CopyInitializable</font>&nbsp;and make use of a single obvious mutating-to-nonmutating implementation of arithmetic operations that would work equally well on all standard numeric types.</div></div></div></blockquote><div><br class=""></div><div><br class=""></div><div>NSNumber is an immutable reference type, so copy just returns a strong reference to itself. So how would a copy initialization of a NSNumber add any value? If we were to add a copy initializer to NSNumber, it would probably be implemented as just replacing self in the init with the other object.</div><div><br class=""></div><div>For reference types there is already a protocol for what you are attempting to do; NSCopying (granted it probably should have a Self return instead of Any… but that is a different can-o-worms).</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><font face="AvenirNext-Regular" class=""><br class=""></font></div><div class=""><font face="AvenirNext-Regular" class="">I’d like to hear opinions regarding this pitch and in case of consensus, I’d write an official proposal and offer it for review.</font></div><div class=""><font face="AvenirNext-Regular" class=""><br class=""></font></div><div class="">Regards,</div><div class="">Gor Gyolchanyan.</div><div class=""><br class=""></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>