Uploaded image for project: 'Swift'
  1. Swift
  2. SR-4227

Can't call mutating protocol extension method in class init

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Duplicate
    • Component/s: Compiler
    • Labels:
      None

      Description

      protocol P {
        associatedtype Header
        var header: Header { get set }
      }
      
      extension P {
        mutating func f() {
          withUnsafePointer(to: &self.header) { print($0) }
        }
      }
      
      class X : P {
        var header: Int = 0
        init() {
          withUnsafePointer(to: &self.header) { print($0) }
      
          // This works:
          var me = self
          me.f()
          
          // But this doesn't:
          self.f() // error: cannot use mutating member on immutable value: 'self' is immutable
        }
      }
      
      X()
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              dabrahams Dave Abrahams
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: