[swift-users] Swift 4 "Cannot use mutating member on immutable value: 'self' is immutable"

Vladimir.S svabox at gmail.com
Mon Sep 18 07:52:14 CDT 2017

On 17.09.2017 13:25, Quinn "The Eskimo!" via swift-users wrote:
> On 15 Sep 2017, at 21:35, Vladimir.S via swift-users <swift-users at swift.org> wrote:
>> … for me it is very strange decision to disallow a method because it is 'expensive'.
> That’s pretty normal for Swift standard library protocols, which define not just the behaviour of the routine but expected performance.  `popFirst()` is expected to be O(1) and that’s not possible with `Array`.
> The rationale behind this decision is, I believe, related to generic algorithms.  If I write generic code that uses `popFirst()`, I can only guarantee the complexity of my code if I can rely on `popFirst()` being O(1).  If someone implements `popFirst()` as O(n), my generic algorithm might go from O(n^2) to O(n^3), which is quite a change.

Do I understand this correctly?: To protect *me* from using popFirst() with possible 
O(n) complexity in my *generic code*, there is no better solution in Swift than just 
*hide* the method from me even in non-generic code. Even if I probably don't care 
about the complexity for my 5 elements array.
And so, I should use hacky code like this(which doesn't produce warnings/errors) to 
fight with compiler:
let x = array[0...].popFirst()

Can't understand/accept this, sorry. If you are saying "this is a current limitation 
we have" - ok, but when you are saying "That’s pretty normal" - I don't understand 
what "normal" means here. I even can understand if compiler can provide us with some 
'popFirstNComplexity()'(or warning with a way to silence it) when 'popFirst' 
shouldn't be accessible because of reasons mentioned by you. But just hide the method.

Also, could you clarify, why .removeFirst() is different? We can use it without problems:

var array = [1, 2, 3, 4, 5]
array.removeFirst() // no warnings
print(array) // [2,3,4,5]


> On 16 Sep 2017, at 01:44, Rick Mann via swift-users <swift-users at swift.org> wrote:
>> Is the compiler looking at the name "pop" and adding additional constraints (and then spewing a bogus error message)?
> I’m not sure what’s going on here mechanically but, yes, the error message is bogus.  This is exactly what SR-5515 is talking about.
> If I were in your shoes I’d call this method something other than `popFirst()`.  This falls under my standard “if you change the semantics, change the name” rule.  Your implementation of `popFirst()` doesn’t conform to the semantics of `popFirst()` — it’s O(n) because `removeFirst()` is O(n) — and thus you want to avoid calling it `popFirst()`.
> Share and Enjoy
> --
> Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
> Apple Developer Relations, Developer Technical Support, Core OS/Hardware
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

More information about the swift-users mailing list