[swift-evolution] When to use argument labels, part DEUX

Dave Abrahams dabrahams at apple.com
Mon Feb 8 07:48:51 CST 2016


on Mon Feb 08 2016, Brent Royal-Gordon <swift-evolution at swift.org> wrote:

>> Agree that basing the preposition location on whether there is a
>> default value is unfortunate. The problem is "Zone" is not
>> redundant/needless when calling it with the default value.
>> 
>> copyWith()
>> 
>> If I were asking "what zone?" Ok it's the default zone, but I'm just asking "with what?"
>
> Honestly, I don't think `-copyWithZone:` is a good API to use as an
> exemplar. It's a non-mutating method with an imperative name, which is
> not usually done even in Objective-C, let alone Swift. 

Arguably you can read it as a noun-phrase.

> In fact, it's so unusual that it doesn't even have the same retaining
> conventions as most other methods.

In Swift/under ARC it does.

> I would consider special-casing `-copyWithZone:` to import as
> `copied(to:)`. (The mutable variant should be `mutablyCopied(to:)`.)
> `to` is used here because, when there *is* a zone in use, the new copy
> is made in that zone. Alternatively, you could think of the zone as an
> option, in which case it'd be `copied(zone:)`. Plain `-copy` should
> simply be hidden; it's a convenience wrapper around `-copyWithZone:`,
> and one that's unnecessary if the zone argument carries a default
> value.

Your points about copyWithZone being unusual are well-taken, but it
still presents a set of API naming issues that are fairly common.  If
you prefer, you can imagine that encodeWithCoder had a default argument.
Any method called xxxWithOptions would be a realistic example (though I
can't remember a specific one offhand), because all option sets are
getting an empty default.

-- 
-Dave



More information about the swift-evolution mailing list