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-2464] Reference types can't call mutating functions declared in a protocol extension. #45069

drewcrawford opened this issue Aug 23, 2016 · 1 comment


Copy link

drewcrawford commented Aug 23, 2016

Previous ID SR-2464
Radar None
Original Reporter @drewcrawford
Type Bug
Status Resolved
Resolution Duplicate

Both Xcode Version 7.3.1 (7D1014) and Xcode Version 8.0 beta 6 (8S201h)

Additional Detail from JIRA
Votes 0
Labels Bug
Assignee None
Priority Medium

md5: ad869a2967de9d14c2e91b020106cb51


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

Issue Description:

Surprisingly, I can't find another bug open for this.

Suppose we have

import Cocoa

protocol Bar {
    var foo: NSViewController? { get set }

We then declare a function in a protocol extension to clear our variable. The function must be mutating or else the variable can't be cleared.

extension Bar {
    mutating func _clear() {
        if let p = foo {
            foo = nil

We can both implement and consume this protocol from a value type

struct Quz: Bar {
    var foo: NSViewController? = nil
    mutating func qux() {

However the same is not true for a reference type

final class Baz: NSViewController {
    var foo: NSViewController? = nil
    override func viewWillAppear() {
        self._clear() //error: cannot use mutating member on immutable value: 'self' is immutable

There is actually no real workaround to this example. In general, the func can be made non-mutating (which doesn't work here) or the protocol can be constrained to reference types (preventing Qux from implementing it), or the reference type can be converted to a value type (not for subclasses of NSViewController).

Copy link

belkadan commented Aug 23, 2016

This is currently behaving as designed, but I think we probably need to change the design.

@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