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

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


> Le 11 déc. 2015 à 20:44, Slava Pestov <spestov at apple.com> a écrit :
> 
> 
>> On Dec 11, 2015, at 11:43 AM, Gwendal Roué <gwendal.roue at gmail.com <mailto:gwendal.roue at gmail.com>> wrote:
>> 
>> 
>>> Le 11 déc. 2015 à 20:34, Slava Pestov via swift-evolution <swift-evolution at swift.org <mailto: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 */
>> 	    value.awakeFromFetch()
>> 	    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é
>> 
> 
> I guess the question is, does it even make sense to write a protocol that can be adopted by both a struct and a class, if the protocol has mutating members?
> 
> Slava

BTW, we have three workarounds to the initial problem so far, using the current state of Swift:

1. the one by Kevin Ballard below in the thread (the best one)
2. my first work around at https://bugs.swift.org/browse/SR-142 <https://bugs.swift.org/browse/SR-142>
3. declare a non-mutating protocol that inherits from the mutating one, and let classes adopt the non-mutating one, as in:

	protocol MutableP {
	  mutating func f()
	}
	protocol P : MutableP {
	  func f()
	}

The last workaround is a different in that it leaves room for structs that decide not to mutate in their implementation of the protocol.

You have an example of this approach at https://github.com/groue/GRDB.swift#databasepersistable-protocol

Gwendal Roué

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


More information about the swift-evolution mailing list