.appending method in the standard library for arrays

Strange there's no appending method on immutable arrays apart from using a plus operator.

2 Likes

+ is the outlier, isn’t it. There’s no similar methods on Sets or Dictionaries either, though NSArray and NSSet and NSDictionary have some.

If I were to assign meaning to this, it would be that the lack of copy-and-change methods encourages people to use mutation, which is often more efficient / easier to optimize. But sometimes you really do just want “the same dictionary with one value removed” or whatever.

3 Likes

Speaking of, given that you can't look up operator definitions anymore, where are these overloads defined? I didn't think they were in the standard library!

(1...2) + [3, 4]
["🍔"] + CollectionOfOne("🍟")

These come from RangeReplaceableCollection, which offers a few overloads:

The first two handle arbitrary Sequences on either side of the + (which is what allows your first example to work — ClosedRange is a Sequence, but not a RangeReplaceableCollection), and the last handles combining two RangeReplaceableCollections together.

This might be a reasonable place to start adding -ing methods to.

1 Like