[swift-evolution] Proposal: Intermediate mutation qualifier for protocol functions on reference-types

Gwendal Roué gwendal.roue at gmail.com
Fri Dec 11 13:43:48 CST 2015

> Le 11 déc. 2015 à 20:34, Slava Pestov via swift-evolution <swift-evolution at swift.org> a écrit :
> For this reason I’m in favor of going in the opposite direction, and prohibiting classes from conforming to protocols with mutating requirements.

This would go too far.

It’s common to write a protocol with mutating methods just because the protocol designer expects that some adopting structs may want to mutate in their implementation. And in this case the goal of the protocol designer is certainly not to limit the protocol adoption to structs.

Here is an example:

	protocol DatabaseFetchable {
	  mutating func awakeFromFetch()
	extension DatabaseFetchable {
	  func fetch() -> Self {
	    var value = /* details omitted */
	    return value

The protocol does not care at all if awakeFromFetch mutates or not. But the protocol designer does: if the awakeFromFetch method were not declared mutating, many structs could simply not adopt it.

Gwendal Roué

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/fe44f006/attachment.html>

More information about the swift-evolution mailing list