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

Vladimir.S svabox at gmail.com
Thu Sep 14 07:14:53 CDT 2017


On 14.09.2017 11:14, Quinn "The Eskimo!" via swift-users wrote:
> 
> On 14 Sep 2017, at 03:56, somu subscribe via swift-users <swift-users at swift.org> wrote:
> 
>> popFirst is not available in the Array …
> 
> Right.  This makes sense when you consider the standard setup for an array, namely, a variable length buffer of items.  Removing the first element is expensive, whereas removing the last element (`popLast()`) is cheap.
> 
> If you run your simplified example in Xcode 8 you get an error that hints at what’s going on.
> 
> var array = [1, 2, 3, 4, 5]
> _ = array.popFirst()
>      ~~~~~~^~~~~~~~
> error: 'ArraySlice<Int>' is not convertible to '[Int]'
> 

Sorry, could you clarify, I can't understand.
What should be the error message here and why at all we should have an error here?

Are we forced to use this code to pop first element in array? :
var array = [1, 2, 3, 4, 5]
let x = array[0...].popFirst()
print(x!) // 1
print(array) // [2,3,4,5]

And why we shouldn't be allowed to have popFirst() for array? Yes, it is expensive, 
but is it a right reason to disallow it totally? In many situations we have a small 
arrays and don't care so much how fast the first element will be pop'ed.
I hope I'm just missing something.

Vladimir.

> Notably, I put Rick’s code into Xcode 8 (Xcode 8.3.3 on macOS 10.12.6 with a new command line tool project) and I get the same error there.
> 
> let mf = self.pendingFetchers.popFirst()
>           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
> error: 'ArraySlice<ModelFetcher>' is not convertible to '[ModelFetcher]’
> 
>                     *                   *                   *
> 
> @Rick, there’s two things in play here:
> 
> * The diagnostic is clearly bogus and you should definitely file a bug about that.
> 
> <https://bugs.swift.org/>
> 
> Please post your bug number, just for the record.
> 
> * You wrote:
> 
>> This code compiled fine in Xcode 8 …
> 
> My tests indicate that it doesn’t.  I suspect that you changed something else during the Swift 4 migration and that’s why you’re seeing it fail.  Can you take another look at the original Xcode 8 code to see what’s else got changed?
> 
> 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