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

Slava Pestov spestov at apple.com
Fri Dec 11 13:44:43 CST 2015


> On Dec 11, 2015, at 11:43 AM, Gwendal Roué <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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/a6635eaa/attachment.html>


More information about the swift-evolution mailing list