New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Name and label changes for closure parameters (SE-0118) #2981
Conversation
filter(suchThat isIncluded: => where(isIncluded:
could be seen by some as dangerous close to:
|
102d1ae
to
8f54eb0
Compare
Glad to see that this has the core team's attention. I was so confused by the name Could I make one suggestion? There are several instances where it seems you're fishing for a synonym or a closely related concept in order to supply another verb. By this I mean: "sort, ordering by," or "split, separating where." While it's true that you sort by ordering, or split by separating, perhaps these could be more terse? I include among this "reduce, accumulating result by"--although "reduce" is not self-explanatory, if we're going to regard it as a term of art then there is no other way one may reduce. Edit: it's internal, but |
on Mon Jun 13 2016, Xiaodi Wu <notifications-AT-github.com> wrote:
Specific suggestions are welcome. We tried really hard to find good names.
Please see the “Further Changes to Consider” section in the pull request text. Edit: I don’t know why I chose not to use
|
on Fri Jun 10 2016, karwa <notifications-AT-github.com> wrote:
No, I don't think it is a problem, but we might want to warn and offer a
Could be, but this time it's just a matter of seeing the wrong analogy.
Dave |
For verb-functions which take a single closure (such as sort), maybe a more verbose, obvious return type would be better than another verb as an argument label? Something like this, perhaps:
Then you'd call it like:
|
on Mon Jun 13 2016, Jordan Rose <notifications-AT-github.com> wrote:
Of course. But please accompany your unhappiness with constructive |
on Mon Jun 13 2016, karwa <notifications-AT-github.com> wrote:
Changing the default types used ordering comparisons is out-of-scope for Dave |
On Mon, Jun 13, 2016 at 7:08 PM, Dave Abrahams notifications@github.com
Yes, and I think they are an improvement in terms of readability. TBH,
My initial suggestion would have been Please see the “Further Changes to Consider” section in the [pull
Maybe |
Ha, sorry, I deleted my message after rereading the title and description. My biggest concern was that I really don't think changing the names of I don't mean that changing those names is out of the question, but that either you should make that the focus of the proposal (and think about all of the major functional operations together), or you should do the argument labels ahead of time. Here's some initial thoughts on specific changes:
That's all very negative, but really I'm glad that you've decided this is worth thinking about, and that the argument names will all be consistent and descriptive after this, and that I liked all the other names. |
A label is needed because otherwise the argument reads as a direct object.
IMO that label should be reserved for usage like
I don't see how the first argument can read correctly as a direct object in this case either. |
On Mon, Jun 13, 2016 at 10:09 PM, Dave Abrahams notifications@github.com
Interesting. I've always read unlabeled predicates as adverbs.
Edit: The same could be said for
Yeah I'm a little stumped here for the moment.
|
on Mon Jun 13 2016, Jordan Rose <notifications-AT-github.com> wrote:
Now that evolution has taken up
I think we considered
Exactly. The purpose of
I don't understand what your problem is here. The word “element” is
It's worse because it's ambiguous, with the likely interpretation
The fact that this one is too long is an argument for changing “reduce”
I don't see how that makes any sense at all. The closure isn't Dave |
8f54eb0
to
ace04d6
Compare
I remember it was mentioned during our discussions, but don't remember specific arguments against it, apart from, maybe, that because What I remember clearly is that |
b794baa
to
8f65539
Compare
3753b8a
to
590a60a
Compare
@swift-ci Please test |
35814e4
to
a7421ca
Compare
451f464
to
52072a0
Compare
@swift-ci Please test and merge |
52072a0
to
bd3e781
Compare
@swift-ci Please test and merge |
bd3e781
to
6105710
Compare
@swift-ci Please test and merge |
6105710
to
090bbf4
Compare
@swift-ci Please test and merge |
The failure is due to a race condition, so I’m merging. |
…y) (apple#2981)" This reverts commit 1840690.
Merged in as #3589 |
Dave, what about these failures? TestFoundation/TestNSString.swift:1094:56: error: incorrect argument label in call (have 'with:isEquivalent:', expected 'with:by:') Are you running build-toolchain on linux before merging? |
Motivation
The names and argument labels of the standard library's closure parameters have been chosen haphazardly, resulting in documentation comments that are inconsistent and often read poorly, and in code completion/documentation that is less helpful than it might be. Because of trailing closure syntax, the choice of argument labels for closures has less impact on use-sites than it might otherwise, but there are many contexts in which labels still do appear, and poorly-chosen labels still hurt readability.
Overview Of Usage Changes
Note: this summary does not illustrate parameter name changes that have also been made pervasively and have an effect on usability of documentation and code completion. Please see the diffs for examples of those.
s.withUtf8Buffer(invoke: processBytes)
s.withUtf8Buffer(processBytes)
lines.split(
isSeparator: isAllWhitespace)
lines.split(
whereSeparator: isAllWhitespace)
words.sort(isOrderedBefore: >)
words.sort(by: >)
words.sorted(isOrderedBefore: >)
words.sorted(by: >)
smallest = shapes.min(
isOrderedBefore: haveIncreasingSize)
smallest = shapes.min(
by: haveIncreasingSize)
largest = shapes.max(
isOrderedBefore: haveIncreasingSize)
largest = shapes.max(
by: haveIncreasingSize)
if a.lexicographicallyPrecedes(
b,
isOrderedBefore: haveIncreasingWeight)
if a.lexicographicallyPrecedes(
b, by: haveIncreasingWeight)
ManagedBuffer<Header,Element>.create(
minimumCapacity: 10,
initialValue: makeHeader)
ManagedBuffer<Header,Element>.create(
minimumCapacity: 10,
makingValueWith: makeHeader)
if roots.contains(isPrime) {
if roots.contains(where: isPrime) {
if expected.elementsEqual(
actual, isEquivalent: haveSameValue)
if expected.elementsEqual(
actual, by: haveSameValue)
if names.starts(
with: myFamily,
isEquivalent: areSameExceptForCase) {
if names.starts(
with: myFamily,
by: areSameExceptForCase) {
let sum = measurements.reduce(0, combine: +)
let sum = measurements.reduce(0, +)
UTF8.encode(
scalar, sendingOutputTo: accumulateByte)
UTF8.encode(
scalar, into: accumulateByte)
Future Extensions
The process of making these changes revealed other, very compelling, naming improvements that could be made, e.g.
xs.filter(suchThat: isPrime)
becomingxs.where(isPrime)
, which, in addition to being much less awkward, fully clarifies the polarity of the argument. These are considered out of scope for this proposal, but should be considered soon afterward. Once we have started down the path of diverging from terms of art for functional algorithms,reduce(_ initialResult, accumulatingResultBy nextPartialResult:)
to something likeaccumulating(startingFrom initialAccumulator:combiningBy nextAccumulator)
just based on the description in the documentation and on the argument labels.mapping(_ transformation)
,flatMapping(_ transformation)
, andflattened()
.contains(where: isPrime)
should becomecontainsAny(where: isPrime)
. It certainly reads better when trailing closure syntax is used.What's in this pull request?
Resolved bug number: (SR-)
Before merging this pull request to apple/swift repository:
Triggering Swift CI
The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:
Smoke Testing
Validation Testing
Lint Testing
Note: Only members of the Apple organization can trigger swift-ci.