> On May 7, 2016, at 3:53 PM, Dave Abrahams <dabrah...@apple.com> wrote: > > > on Sat May 07 2016, Matthew Johnson <matthew-AT-anandabits.com> wrote: > >> Sent from my iPad >> >> On May 7, 2016, at 2:21 AM, Andrew Trick via swift-evolution >> <swift-evolution@swift.org> wrote: >> >> On May 6, 2016, at 5:48 PM, Dave Abrahams via swift-evolution >> <swift-evolution@swift.org> wrote: >> >> I don’t mean to imply that it is the *only* valuable >> property. However, it I (and many others) do believe it is an >> extremely valuable >> property in many cases. Do you disagree? >> >> I think I do. What is valuable about such a protocol? What generic >> algorithms could you write that work on models of PureValue but don't >> work just as well on Array<Int>? >> >> class Storage { >> var element: Int = 0 >> } >> >> struct Value { >> var storage: Storage >> } >> >> func amIPure(v: Value) -> Int { >> v.storage.element = 3 >> return v.storage.element >> } >> >> 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, the >> developer >> will need to implement CoW, or we need add some language features. >> >> Thank you for this clear exposition of how PureValue relates to pure >> functions. >> This is the exact intuition I have about it but you have stated it much more >> clearly. >> >> Language features to help automate CoW would be great. It would eliminate >> boilerplate, but more importantly it would likely provide more information to >> the compiler. > > Whoa; Andy never suggested this would help automate CoW. Are you > suggesting that? How would it work?
Quoting Andy: "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, the developer will need to implement CoW, or we need add some language features." I was referring to new language features that eliminate the need for the developer to implement CoW manually while preserving the same semantics. 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”. > > -- > -Dave
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution