Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-4229] Cannot use mutating member on immutable value: 'self' is immutable #46812

aciidb0mb3r opened this issue Mar 13, 2017 · 2 comments


Copy link

aciidb0mb3r commented Mar 13, 2017

Previous ID SR-4229
Radar None
Original Reporter @aciidb0mb3r
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 89dcbdfbbd2ca57390d28d060b60ed39


  • SR-142 mutating function in protocol extension erroneously requires var declaration of class variables

Issue Description:

Is this error correct? If yes, how to work around it because mutating doesn't work on classes

protocol Foo {
    mutating func restoreState()

extension Foo {
    mutating func restoreState() {}

class Bar: Foo {
    func foo() {
Copy link

belkadan commented Mar 14, 2017

It's sort of correct. :-/ The problem is that mutating methods implemented in protocol extensions are allowed to re-assign self, which is something that doesn't exactly make sense for a class: it will reassign the reference, but not change any other references. There's a whole family of issues that show that protocols with mutating methods produce surprising and potentially-bug-prone results with classes.

You can work around this by using a temporary var:

var mutableSelfHack = self

but you may want to reconsider whether it makes sense for a class to conform to a protocol with mutating methods at all.

Copy link

belkadan commented Mar 14, 2017


@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

2 participants