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

Vladimir.S svabox at gmail.com
Fri Sep 15 06:08:41 CDT 2017


On 14.09.2017 22:02, Jon Shier via swift-users wrote:
> Looks like this is sort of tracked in  SR-5515popFirst error message is silly 
> <https://bugs.swift.org/browse/SR-5515>. I’m not sure what a good answer is here, 
> since it seems like it isn’t supposed to be available if it can’t guarantee O(1) 
> behavior. Still seems like Array should have it, but in any case, this looks like a 
> bad error message at least.

But can .removeFirst() guarantee this? I don't believe these two methods a very 
different in implementation. And, as I said, for me it is very strange decision to 
disallow a method because it is 'expensive'. I hope this is just a bug, that will be 
fixed.

Vladimir.

> 
> 
> 
> Jon
> 
> 
>> On Sep 14, 2017, at 11:05 AM, Jon Shier via swift-users <swift-users at swift.org 
>> <mailto:swift-users at swift.org>> wrote:
>>
>> This seems oddly similar to https://bugs.swift.org/browse/SR-5659, but [Thing] 
>> should meet the appropriate criteria for popFirst to work properly. Yet the error 
>> we’re seeing here is the same that was originally seen when trying to use popFirst 
>> on a String. Definitely feels like some sort of Swift bug here.
>>
>>
>>
>> Jon
>>
>>
>>> On Sep 14, 2017, at 10:48 AM, Jon Shier via swift-users <swift-users at swift.org 
>>> <mailto:swift-users at swift.org>> wrote:
>>>
>>> It also doesn’t explain why removeFirst would work but popFirst doesn’t. The 
>>> mutation to the underlying array should be the same, the only different is that 
>>> one returns the element optionally. It also doesn’t explain why popFirst doesn’t 
>>> show up I the autocomplete but compiles anyway.
>>>
>>>
>>>
>>> Jon
>>>
>>>> On Sep 14, 2017, at 8:16 AM, Vladimir.S via swift-users <swift-users at swift.org 
>>>> <mailto:swift-users at swift.org>> wrote:
>>>>
>>>> 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 
>>>>> <mailto: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 <mailto:swift-users at swift.org>
>>>>> https://lists.swift.org/mailman/listinfo/swift-users
>>>> _______________________________________________
>>>> swift-users mailing list
>>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>>> https://lists.swift.org/mailman/listinfo/swift-users
>>>
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users
> 
> 
> 
> _______________________________________________
> 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