# Non-mutating setters

**URL:** <https://forums.swift.org/t/non-mutating-setters/14379>\
**Category:** Using Swift\
**Created:** [July 11, 2018, 2:05am UTC](https://forums.swift.org/t/non-mutating-setters/14379 "2018-07-11T02:05:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chenyungui](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chenyungui/32/46119_2.png) [@Chenyungui](https://forums.swift.org/u/Chenyungui)\
**Post date:** [July 11, 2018, 2:05am UTC](https://forums.swift.org/t/non-mutating-setters/14379/1 "2018-07-11T02:05:01Z")

</div>

```swift
public enum TutorialType : String {
    case tutorial1
    case tutorial2
}

extension TutorialType {
    public var displayed: Bool {
        get { return UserDefaults.standard.bool(forKey: self.rawValue) }
        set { UserDefaults.standard.set(newValue, forKey: self.rawValue) }
    }
}

func showTutorial(_ type: TutorialType) {
    // 1. show the tutorial
    // ...
    // 2. set displayed to true
    type.displayed = true //cannot assign to property: 'type' is a 'let' constant
}

```

Hi:  
As above, I have added a non-mutating compute attribute 'displayed' to TutorialType.  
But when I use it in 'showTutorial(\_:)' , the compiler will requires to me change `type` to a mutable variable. I think it should be valid to call 'type.displayed = true', is it a compiler bug? Any thoughts?

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [July 11, 2018, 2:24am UTC](https://forums.swift.org/t/non-mutating-setters/14379/2 "2018-07-11T02:24:34Z")

</div>

You can declare the setter `nonmutating`:

```swift
extension TutorialType {
    public var displayed: Bool {
        get { return UserDefaults.standard.bool(forKey: self.rawValue) }
        nonmutating set { UserDefaults.standard.set(newValue, forKey: self.rawValue) }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Chenyungui](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chenyungui/32/46119_2.png) [@Chenyungui](https://forums.swift.org/u/Chenyungui)\
**Post date:** [July 11, 2018, 4:08am UTC](https://forums.swift.org/t/non-mutating-setters/14379/3 "2018-07-11T04:08:07Z")

</div>

Oh, I never knew this ! Thank you~

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 11, 2018, 7:24am UTC](https://forums.swift.org/t/non-mutating-setters/14379/4 "2018-07-11T07:24:35Z")

</div>

The same way you can declare the getter `mutating`. The rule of thumb is that the getter is `nonmutating`, and setter `mutating` by default.

---

<div class="post-metadata">

**Author:** ![fabb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fabb/32/2230_2.png) [@fabb](https://forums.swift.org/u/fabb)\
**Post date:** [July 16, 2018, 4:37pm UTC](https://forums.swift.org/t/non-mutating-setters/14379/5 "2018-07-16T16:37:36Z")

</div>

This looks so wrong... well maybe not wrong, but more like class semantics.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [July 16, 2018, 4:44pm UTC](https://forums.swift.org/t/non-mutating-setters/14379/6 "2018-07-16T16:44:02Z")

</div>

It isn't unreasonable for structs to have reference-like behavior. Pointers are one obvious case.
