<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 7, 2016, at 3:53 PM, Dave Abrahams <<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class="">on Sat May 07 2016, Matthew Johnson <<a href="http://matthew-at-anandabits.com" class="">matthew-AT-anandabits.com</a>> wrote:<br class=""><br class=""><blockquote type="cite" class="">Sent from my iPad<br class=""><br class="">On May 7, 2016, at 2:21 AM, Andrew Trick via swift-evolution<br class=""><<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""> On May 6, 2016, at 5:48 PM, Dave Abrahams via swift-evolution<br class=""> <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""> I don’t mean to imply that it is the *only* valuable<br class=""> property. However, it I (and many others) do believe it is an<br class=""> extremely valuable<br class=""> property in many cases. Do you disagree?<br class=""><br class=""> I think I do. What is valuable about such a protocol? What generic<br class=""> algorithms could you write that work on models of PureValue but don't<br class=""> work just as well on Array<Int>?<br class=""><br class=""> class Storage {<br class=""> var element: Int = 0<br class=""> }<br class=""><br class=""> struct Value {<br class=""> var storage: Storage<br class=""> }<br class=""><br class=""> func amIPure(v: Value) -> Int {<br class=""> v.storage.element = 3<br class=""> return v.storage.element<br class=""> }<br class=""><br class=""> I (the optimizer) want to know if 'amIPure' is a pure function. The<br class=""> developer needs to tell me where the boundaries of the value lie. Does<br class=""> 'storage' lie inside the Value, or outside? If it is inside, then Value is a<br class=""> 'PureValue' and 'amIPure' is a pure function. To enforce that, the developer<br class=""> will need to implement CoW, or we need add some language features.<br class=""><br class="">Thank you for this clear exposition of how PureValue relates to pure functions.<br class="">This is the exact intuition I have about it but you have stated it much more<br class="">clearly.<br class=""><br class="">Language features to help automate CoW would be great. It would eliminate<br class="">boilerplate, but more importantly it would likely provide more information to<br class="">the compiler.<br class=""></blockquote><br class="">Whoa; Andy never suggested this would help automate CoW. Are you<br class="">suggesting that? How would it work?<br class=""></div></div></blockquote><div><br class=""></div><div>Quoting Andy:</div><div><br class=""></div><div>"<span style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">I (the optimizer) want to know if 'amIPure' is a pure function. The developer needs to tell me where the boundaries of the value lie. Does 'storage' lie inside the Value, or outside? If it is inside, then Value is a 'PureValue' and 'amIPure' is a pure function. To enforce that, <b class="">the developer will need to implement CoW, or we need add some language features</b>.</span>"</div><div><br class=""></div><div>I was referring to new language features that eliminate the need for the developer to implement CoW manually while preserving the same semantics. </div><div><br class=""></div><div>I don’t know about the general case, but in simple cases I can imagine a feature such as “indirect struct” or Box<T: ValueType> which would contain a reference to a struct on the heap. Any time a mutating operation was performed on a non-uniquely referenced struct it would be copied first and the internal reference updated to point to the new copy on the heap. This is the kind of thing I had in mind when I said “automating CoW”.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">-- <br class="">-Dave<br class=""></div></div></blockquote></div><br class=""></body></html>