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

Quinn "The Eskimo!" eskimo1 at apple.com
Thu Sep 14 03:13:58 CDT 2017


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]'

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




More information about the swift-users mailing list