[swift-evolution] [swift-evolution-announce] [Review] SE-0118: Closure Parameter Names and Labels

Jordan Rose jordan_rose at apple.com
Thu Jul 7 18:03:21 CDT 2016

[Proposal: https://github.com/apple/swift-evolution/blob/master/proposals/0118-closure-parameter-names-and-labels.md ]

Hi, Dave, Dmitri, Max. Sorry I didn’t make the pre-review commentary thread. I find I’m still not happy with several of these names, although there are many other improvements. Stepping back, I think the Swift API guidelines just don’t do a good job with closure parameters. We should have naming guidelines for strategy closures and for callbacks.

lines.split(whereSeparator: isAllWhitespace)

This reads like an English sentence, but it doesn’t have the correct meaning for me. This implies a structure that has a pre-existing “separator", and checks if that separator matches the predicate, rather than searching for an element that matches the predicate, and splitting on that. I realize that the former reading doesn’t make much sense as a function, but it’s still impeded my understanding more than helping it along.

Alternate suggestions: split(where:), split(separatingWhere:).

The sort(by:) family

…seems fine, is slightly confusable with a “sort by key” API, but at least has a different type signature.

if roots.contains(where: isPrime) {

I really don’t like this one; it feels like it’s missing a noun. It certainly isn’t a valid English phrase. If we were naming an intersects(_:)-like feature, we might call it `roots.containsAny(of: primes)` or `roots.contains(anyOf: primes)`.

Alternate suggestions: containsAny(where:), contains(anyWhere:)

if expected.elementsEqual(actual, by: haveSameValue)

I tend to agree with the pre-review commentary <http://thread.gmane.org/gmane.comp.lang.swift.evolution/22188> that “by:” doesn’t actually fit here. The difference between elementsEqual(_:by:) / starts(with:by:) and the sort(by:) family is that “sort by…” and “order by…” are both things people actually say, but “equal by…” is not. Then again, when they say those things, they’re either talking about a key (“sort by name”) or a general comparison (“sort by compar[ing] names”, <=> rather than <).

let sum = measurements.reduce(0, +)

It concerns me that we can’t think of a label for this operation; it implies we can’t think of an English sentence to describe it. “Reduce this collection to a single element by combining using +.” reduce(startingWith:combiningWith:) is pretty unwieldy, though.

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

More information about the swift-evolution mailing list