<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 5, 2016, at 11:06 AM, Joe Groff 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=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class="Apple-interchange-newline">On May 5, 2016, at 8:56 AM, Dave Abrahams &lt;<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>&gt; wrote:<br class=""><br class=""><br class="">on Wed May 04 2016, Joe Groff &lt;<a href="http://jgroff-at-apple.com" class="">jgroff-AT-apple.com</a>&gt; wrote:<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On May 4, 2016, at 5:28 AM, T.J. Usiyan via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Something about your first paragraph reminded me of a question I've<br class="">had for a while. Is there a reasoning behind not being able to<br class="">restrict a protocol to value types? One way that this might be<br class="">workable is if we could overload protocols for Value vs for<br class="">reference.<br class=""></blockquote><br class="">I'm not totally comfortable with this, because references are a kind<br class="">of value.<span class="Apple-converted-space">&nbsp;</span><br class=""></blockquote><br class="">We're using the word “value” in 3 different ways now. &nbsp;If we don't sort<br class="">them out, this is going to become impossible to resolve. &nbsp;So let me<br class="">propose some terms:<br class=""><br class="">1. Joe's sense, i.e. the compiler-implementation-level sense, in which a<br class="">&nbsp;value of type T is essentially what an UnsafePointer&lt;T&gt; points at.<br class="">&nbsp;In this sense, a variable of any type T “has a value.” &nbsp;Even though<br class="">&nbsp;it's not strictly correct (since the term really applies to<br class="">&nbsp;expressions), I propose “rvalue” for this one. &nbsp;<br class=""><br class="">2. The “value types vs reference types” sense, where every type falls<br class="">&nbsp;into one of two crisp buckets based on how it's declared. &nbsp;I propose<br class="">&nbsp;we always say “reference type” or “value type” for this one.<br class=""><br class="">3. The “value semantics vs reference semantics” sense, where a type's<br class="">&nbsp;category depends on how it is implemented, &nbsp;and it's possible (though<br class="">&nbsp;inadvisable) to fall outside either bucket. &nbsp;This is the only<br class="">&nbsp;interesting category when you're discussing protocols and<br class="">&nbsp;constraints, and doesn't have any intrinsic relationship to sense<br class="">&nbsp;#2. &nbsp;I propose we always say “value semantics” or “reference<br class="">&nbsp;semantics” for this one.<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">I claim that my sense is the same as #2. A reference has all the properties of a value-type value; it has the additional ability to be used as a handle to access independent program state. Int isn't any less of a value because it can be used to reference values in an array. Rather than try to break the world down according to categories of types, I think it's more effective to look at the semantics of *operations*. So called "reference types" have value semantics operations too; reassigning a class reference, '+='-ing a pointer, and adding or removing elements from an Array of references are all operations that mutate only the value being operated on, even though none of these types are strictly "value types”.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div><div>The distinction between “reference as a value” and “value the reference points to” is a pretty important one. &nbsp;All of the operations you discuss are really operations on the “reference as a value”.&nbsp;</div><div><br class=""></div><div>I think the reason we talk about *types* rather than *operations* having value semantics is because that tells us that *all* operations on any instance are going to have value semantics. &nbsp;This means that *nobody else* is going to mutate the variable in question behind our back. &nbsp;If the type has “reference semantic operations” we don’t have that guarantee today. &nbsp;The fact that the type may also have some "value semantic operations" seems relatively unimportant.</div><div><br class=""></div><div>One possible avenue for improvement in the language where this distinction could be useful is by allowing us to specify which operations have "value semantics" on reference types and allow us to designate specific *references* as being value semantic. &nbsp;`let` wouldn’t work as that only means that the reference itself is immutable, not the value it refers to. &nbsp;We would need some additional syntax to specify that the value the reference points to cannot be mutated. &nbsp;Such a reference could only be assigned to when the compiler can prove it will refer to a value that has *no* existing mutable references. &nbsp;This would allow us to capture the benefits of value semantics for specific *instances* of a reference type.</div><div><br class=""></div><div>-Matthew</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">-Joe</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class="">I can see value in there being some kind of PureValue protocol, for<br class="">types that represent fully self-contained values, but conforming to<br class="">that protocol requires a bit more thought than just being a struct or<br class="">enum, since there are structs that have reference semantics (such as<br class="">UnsafePointer), and there are hybrid value types that contain<br class="">references to data that isn't part of the value (an Array&lt;Class&gt;, for<br class="">instance).<br class=""><br class="">-Joe<br class=""><br class=""><blockquote type="cite" class="">TJ<br class=""><br class="">On Tue, May 3, 2016 at 11:02 PM, Jordan Rose &lt;<a href="mailto:jordan_rose@apple.com" class="">jordan_rose@apple.com</a>&gt; wrote:<br class="">Dave and I have pondered this before, and considered that one possible (drastic) solution is to ban classes from implementing protocols with mutating members, on the grounds that it’s very hard to write an algorithm that’s correct for both.<br class=""><br class="">func removing(_ element: Element) -&gt; Self {<br class="">var result = self // not necessarily a copy…<br class="">result.remove(element)<br class="">return result // not necessarily an independent value<br class="">}<br class=""><br class="">func zapBadElements&lt;C: RangeReplaceableCollection where C.Generator.Element == Int&gt;(_ nums: inout C) {<br class="">// requires inout on ‘nums’ &nbsp;even when it’s a class<br class="">for i in nums.indices {<br class="">&nbsp;&nbsp;if nums[i] &lt; 0 {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;nums.removeAtIndex(i)<br class="">&nbsp;&nbsp;}<br class="">}<br class="">// …because of this.<br class="">if nums.lazy.filter { $0 == 0 }.count &gt; 5 {<br class="">&nbsp;&nbsp;nums = C()<br class="">}<br class="">}<br class=""><br class="">var refCollection: SharedArrayOfSomeKind&lt;Int&gt; = …<br class="">// either the variable ‘refCollection’ or the instance of ‘SharedArrayOfSomeKind’ might be mutated…or both!<br class="">zapBadElements(&amp;refCollection)<br class=""><br class="">There are of course ways to safely use a protocol with mutating requirements with classes, namely if you only use them for mutation (i.e. they’re only called from ‘mutating’ members or on ‘inout’ parameters) and never rely on value copying (no assignment, no returning). Most simple wrappers around mutating members would fall into this category.<br class=""><br class="">We didn’t really develop the idea very far yet because there’s been more pressing things to worry about. I’m bringing it up here because it’s an important idea that shouldn’t get lost.<br class=""><br class="">---<br class=""><br class="">In lieu of this, I and a few others brought up the “incorrect” behavior of reassigning ‘self’ in a protocol extension when the model type is a class, and got shot down. I don’t have those discussions on hand at the moment, but I remember we deliberately decided to leave protocol extensions the way they were, allowing them to reassign class references. I think it’s because it means things like zapBadElements are more likely to work correctly^W as expected―if you don’t have any other references at the time you do the mutation, it can work. But yeah, I’m uncomfortable with the situation we’re in right now.<br class=""><br class="">Jordan<br class=""><br class=""><br class=""><blockquote type="cite" class="">On May 3, 2016, at 13:09, James Froggatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Thanks for the response, I agree this is currently the best<br class="">solution. Unfortunately, it's not just as simple as just<br class="">implementing each method, since without being able to call super, I<br class="">have to fully reimplement the original behaviour, which at best<br class="">seems like bad practice, and would break in future versions of<br class="">Swift, and at worst could lead to hard-to-detect bugs right now.<br class=""><br class="">To recap for anyone reading, protocol extensions currently apply<br class="">mutating methods unmodified to reference types, as I found trying<br class="">to make a reference-type collection. This results in the compiler<br class="">disallowing ‘let’ when calling these functions, and allows methods<br class="">to reassign the reference ‘self’ to a new object. The best solution<br class="">is to manually implement each method, removing the mutating<br class="">modifier, yet this workaround doesn't extend to generic code.<br class=""><br class="">To fix this behaviour, we would need to distinguish between ‘true’<br class="">mutating functions, which reassign self, and ‘partially’ mutating<br class="">functions, for use in generics and protocol extensions, which can<br class="">reassign properties only.<br class="">Is there any support for making this change? Or are there any simpler solutions?<br class=""><br class="">I did submit a bug report, but I'm pretty sure a decent fix is not<br class="">possible without some evolution of the language regarding the<br class="">mutating keyword, so I'm trying to bring this up here in hope of us<br class="">getting an actual solution. I've changed the title to what I hope<br class="">is something that better reflects the problem; this thread was<br class="">originally titled ‘[swift-evolution] [Bug?] Reference types and<br class="">mutating methods’.<br class=""><br class=""><br class="">PS: I have noticed another side-effect of calling mutating<br class="">functions on my reference-type collection: it seems to trigger<br class="">didChange on properties, even when, upon comparing the new and old<br class="">objects, the reference isn't being changed. I haven't done much<br class="">experimentation with this behaviour; this may be an unexpected<br class="">side-effect of an extension method assigning to self, but it feels<br class="">like it could be undefined behaviour.<br class=""><br class="">From James F<br class=""><br class="">On 30 Apr 2016, at 16:38, T.J. Usiyan &lt;<a href="mailto:griotspeak@gmail.com" class="">griotspeak@gmail.com</a>&gt; wrote:<br class=""><br class=""><blockquote type="cite" class="">The problem here seems to be with using the default implementation provided. If you override `append` in ObservedArray, the compiler allows it. That seems 'safe' but odd at first. I wouldn't *want* to implement every mutating method, but that is the current solution. I haven't puzzled out the reasoning behind this myself.<br class=""><br class=""><br class="">``` swift<br class="">class ObservedArray&lt;T&gt; : ArrayLiteralConvertible {<br class="">&nbsp;&nbsp;var value: [T]<br class="">&nbsp;&nbsp;init(value: [T]) {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.value = value<br class="">&nbsp;&nbsp;}<br class="">&nbsp;&nbsp;required init() {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.value = []<br class="">&nbsp;&nbsp;}<br class=""><br class="">&nbsp;&nbsp;required convenience init(arrayLiteral elements: T...) {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.init(elements)<br class="">&nbsp;&nbsp;}<br class=""><br class="">}<br class=""><br class="">extension ObservedArray {<br class="">&nbsp;&nbsp;typealias Index = Int<br class=""><br class="">&nbsp;&nbsp;var startIndex: Index {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return value.startIndex<br class="">&nbsp;&nbsp;}<br class=""><br class="">&nbsp;&nbsp;var endIndex: Index {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return value.endIndex<br class="">&nbsp;&nbsp;}<br class=""><br class="">&nbsp;&nbsp;subscript(position: Index) -&gt; T {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return value[position]<br class="">&nbsp;&nbsp;}<br class=""><br class="">}<br class=""><br class="">extension ObservedArray : RangeReplaceableCollectionType {<br class="">&nbsp;&nbsp;typealias Generator = IndexingGenerator&lt;[T]&gt;<br class=""><br class="">&nbsp;&nbsp;func generate() -&gt; Generator {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return value.generate()<br class="">&nbsp;&nbsp;}<br class="">}<br class=""><br class="">extension ObservedArray {<br class="">&nbsp;&nbsp;func replaceRange&lt;C : CollectionType where C.Generator.Element == Generator.Element&gt;(subRange: Range&lt;Index&gt;, with newElements: C) {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value.replaceRange(subRange, with: newElements)<br class="">&nbsp;&nbsp;}<br class=""><br class="">&nbsp;&nbsp;func append(newElement: T) { // &lt;- adding this makes it work<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value.append(newElement)<br class="">&nbsp;&nbsp;}<br class="">}<br class=""><br class="">let array: ObservedArray&lt;String&gt; = []<br class="">array.append("1")<br class=""><br class=""><br class="">```<br class=""><br class=""><br class=""><br class=""><br class="">On Sat, Apr 30, 2016 at 7:52 AM, James Froggatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">I don't believe this has been addressed, please correct me if I'm wrong.<br class=""><br class="">--My Situation--<br class="">I've recently been working on an observable collection type. Because each stores ‘subscriptions’ to changes that occur, it made sense to me that this should be a reference type, so subscriptions can't be copied with the values themselves.<br class=""><br class="">I have made this class conform to RangeReplaceableCollectionType, providing it with all the standard collection functions. I do the following:<br class=""><br class="">let array: ObservedArray&lt;String&gt; = []<br class="">array.append("1") //Error: Cannot use mutating member on immutable value: ‘array’ is a ‘let’ constant<br class=""><br class="">I have to make the reference immutable just to use my new collection type? This is a bit of a deal-breaker.<br class=""><br class="">--The Problem--<br class="">Mutating methods allow ‘self’ to be reassigned, which is just another way to mutate a value type. However, reassigning ‘self’ has a special meaning to reference types, which is presumably the reason they are disallowed in classes.<br class=""><br class="">However, classes can conform to protocols with mutating methods, leading to the compiler disallowing calls to mutating methods for ‘let’ values of type ‘protocol&lt;MutatingProtocol, AnyObject&gt;’, which can be an annoyance in generic code. In addition, classes can inherit mutating methods from protocol extensions, leading to the behaviour I describe above.<br class=""><br class="">Is this intentional behaviour? Am I going about this in the wrong way? Or is this really an omission in the language?<br class="">_______________________________________________<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=""><br class=""></blockquote>_______________________________________________<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=""></blockquote><br class=""><br class="">_______________________________________________<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=""></blockquote></blockquote><br class="">--<span class="Apple-converted-space">&nbsp;</span><br class="">Dave<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span></div></blockquote></div><br class=""></body></html>