Swift’s nonmutating Keyword

In preparing my recent “Hidden Gems in Swift” talk, I carefully combed through Swift’s “Lexical Structure” section. One keyword that stuck out to me, but ultimately didn’t make it into the talk was nonmutating. Much like the @nonobjc attribute, it surprised me that I hadn’t seen this keyword in the wild.

In searching for example uses, I stumbled upon this conversation between Andy Matuschak and Sidney San Martín:

Stepping through Sidney’s example, we can see how nonmutating signals that a setter doesn’t modify the containing instance, but instead has global side effects.

Whether or not this is good practice is a larger question. But, it’s definitely worth adding to your tool belt!