# Issue with \`mutating\` methods on protocols and \`AnyObject\` conformers

**URL:** <https://forums.swift.org/t/issue-with-mutating-methods-on-protocols-and-anyobject-conformers/62842>\
**Category:** Using Swift\
**Created:** [January 29, 2023, 2:04am UTC](https://forums.swift.org/t/issue-with-mutating-methods-on-protocols-and-anyobject-conformers/62842 "2023-01-29T02:04:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisbia](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chrisbia/32/29915_2.png) [@chrisbia](https://forums.swift.org/u/chrisbia)\
**Post date:** [January 29, 2023, 2:04am UTC](https://forums.swift.org/t/issue-with-mutating-methods-on-protocols-and-anyobject-conformers/62842/1 "2023-01-29T02:04:37Z")

</div>

Not sure if this is a bug or not, but if you have a protocol with a `mutating` method:

```swift
protocol Rearrangeable: Collection {
    mutating func move(from current: Index, to new: Index)
}

```

And define another `AnyObject` protocol that conforms to it:

```swift
protocol ThisIsNotAValueType: AnyObject, Rearrangeable {}

```

If you ever have a generic that conforms to the refined protocol, it won't permit calling the `mutating` method:

```swift
func here<T>(object: T) where T: ThisIsNotAValueType {
    // code ...
    object.move(from: current, to: new)
    // Get error: "Cannot use mutating member on immutable value: 'object' is a 'let' constant"
}

```

This can be circumvented by redefining the `mutating` method requirement in the refined protocol:

```swift
protocol ThisIsNotAValueType: AnyObject, Rearrangeable {

    /// - Note: Added as `Rearrangeable.move(from:to:)` is marked as `mutating`
    /// and so doesn't work with immutable references; even though we're an 
    /// `AnyObject`.
    func move(from current: Index, to new: Index)
}

```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 29, 2023, 2:31am UTC](https://forums.swift.org/t/issue-with-mutating-methods-on-protocols-and-anyobject-conformers/62842/2 "2023-01-29T02:31:09Z")

</div>

> [@Class only protocol - why the examples are not working?](https://forums.swift.org/t/class-only-protocol-why-the-examples-are-not-working/49860/2):
>
> This is because mutating means that self can be reassigned, which is not permitted for let constants even of reference type (i.e., classes). Recall that when you modify a struct member, you modify the whole struct, but this is not the case when you modify a class member. This is a common point of confusion and is discussed at length [here](https://bugs.swift.org/browse/SR-142) and [here](https://forums.swift.org/t/proposal-intermediate-mutation-qualifier-for-protocol-functions-on-reference-types/401) and [here](https://forums.swift.org/t/protocols-mutating-and-value-vs-reference-types/2395) and [here](https://forums.swift.org/t/mutating-function-on-class-type-possible-bug/15066).
