Sequence dropFirst() naming

Hi all,

I’ve ended up using Swift without autocomplete recently and noticed I keep mistyping the method dropFirst() on Sequence types. Is there a reason this isn’t named ‘droppingFirst()’ as might be suggested by this section of the API design guidelines?

Best,
Jared

3 Likes

Just looking at the API guidelines I understand dropFirst() to be a mutating method and that is why the ‘ed’ or ‘ing’ suffix is not added to make it droppingFirst(). In that dropFirst returns a mutated value. Someone please correct me if I am wrong here.

Hi Matt. ‘Mutating’ is typically used to mean that the method mutates the object that it is called on. i.e. if dropFirst were mutating we would expect it to remove the first element from the sequence that it is called on.

dropFirst() is non-mutating and we can quickly verify that in the REPL:

  1> var list = [1, 2, 3]
list: [Int] = 3 values {
  [0] = 1
  [1] = 2
  [2] = 3
}
  2> list.dropFirst()
$R0: ArraySlice<Int> = 2 values {
  [1] = 2
  [2] = 3
}
  3> list
$R1: [Int] = 3 values {
  [0] = 1
  [1] = 2
  [2] = 3
}

The deferred proposal, SE-0132, might be an insightful read.

4 Likes

Thank you, Jared_Khan. I would have understood that method to mutate the original object then based upon the name of dropFirst(). Thus making the point in which you are bringing up in this post.

I totally agree that the name dropFirst is wrong. Maybe it’s the way my brain is wired, but I find such inconsistencies extremely annoying.

I really hope SE-0132 will be revisited sooner rather than later.

1 Like

Yeah. Naming could be better, should be mapping, filtering, etc. I seem to remember that drop, map, etc. we’re declared ‘state-of-the-art’ terms and therefore used instead of the more logical version.

Yeah, the names right now don't match up with the Swift API naming guidelines. I hope this gets revisited soon.

Terms of Service

Privacy Policy

Cookie Policy