[swift-evolution] Support for pure setters

Jessy Catterwaul mr.jessy at gmail.com
Thu Feb 4 12:41:09 CST 2016


> I remain unconvinced that any method that starts with "set" without a matching getter could be turned into a "good property setter". I'm not sure who you're trying to convince with this metric.

Are there counterexamples? I do not have a memory of coming across something that began with “set”, and not thinking that what come after should have been a property or a subscript.


> Maybe you should start by telling if you think that there can be a "bad setter". For instance, can I replace a `sendResponse(String)` method that sends a string over the network with a `response` set-only property?

That’s “send response”, not “setResponse”. Without knowing more about that API, my inclination is to think it would correctly be titled

func send(response: String)


> IMO, a good setter behaves like it's setting a variable, because this is exactly what the assignment operator suggests. That is:
> you should be able to replace a sequence of writes to a setter by a single write with the last value and still have essentially the same end result;
> you should be able to reorder setter operations and still essentially get the same end result.
> 
> "Essentially the same end result" means that it's okay if the logging output is different or if you wasted cycles in the process.

Sounds good to me. I believe all the places that I’ve used set-only properties matched these guidelines.


More information about the swift-evolution mailing list