# Surveying how Swift evolves

**URL:** <https://forums.swift.org/t/surveying-how-swift-evolves/12726>\
**Category:** Discussion\
**Created:** [May 16, 2018, 5:46pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726 "2018-05-16T17:46:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![armcknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/armcknight/32/3486_2.png) [@armcknight](https://forums.swift.org/u/armcknight)\
**Post date:** [May 16, 2018, 5:46pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/1 "2018-05-16T17:46:33Z")

</div>

Hello!

I am developing a survey of Swift utility libraries, with the goal of observing how the language is evolving in the wild. It was rightly pointed out that this could be a fantastic tool to generate new Evolution Proposals. (Writeup, scripts, etc on GitHub at [GitHub - armcknight/colloquial-swift: Writeups and scripts for my survey of Swift utility libraries.](https://github.com/armcknight/colloquial-swift) README is a work in progress!)

**Details on the survey:**

Things I have automated so far:

- search github for Swift repositories matching certain criteria (e.g., name contains "util" or "extension"): 5,810 unique repositories currently returned
- focus on repos with podspecs (maybe carthage and spm one day): 1,357 repositories
- remove example/test/dependency directories: removed about half of over 500K files
- focus on declarations using `swiftc -print-ast` (avoids searching through comments/implementation)
- classify by type of declaration: `func`, `enum`, etc
- especially focus on `extension` declarations and group all declarations they contain

From there I was able to do some quick command-line processing to aggregate and look for patterns. That's the next step that needs to be automated, as well as deciding which questions are actually important (and which ones aren't being asked yet). Here are some early things I found, which I presented recently at iOSDevCampCO.

**Example findings:**

**Note:** for all the lists, the first column of numbers are the frequency with which something was encountered.

- almost 13,000 extension declarations
- top 10 Swift stdlib APIs extended, with the number of extensions declared on each:

> 608 String  
> 206 Array  
> 204 Date  
> 160 Int  
> 119 Dictionary where Key : Hashable  
> 90 Double  
> 86 Data  
> 68 Sequence  
> 64 URL  
> 62 Array where Element : Equatable

I drilled down into String to see how people are extending it. Here are the top 10 function declarations from extensions on String, in their canonical form from swiftc:

> 24 "trim() -\> String"  
> 13 "substring(from: Int) -\> String"  
> 12 "substring(to: Int) -\> String"  
> 11 "isValidEmail() -\> Bool"  
> 10 "trimmed() -\> String"  
> 10 "toBool() -\> Bool?"  
> 10 "height(withConstrainedWidth width: CGFloat, font: UIFont) -\> CGFloat"  
> 9 "trim()"  
> 9 "toDouble() -\> Double?"  
> 9 "isNumber() -\> Bool"

Trimming is not only the most popular function declaration, but appears many times in varied forms (84 total functions dealing with trimming):

> 24 "trim() -\> String"  
> 10 "trimmed() -\> String"  
> 9 "trim()"  
> 3 "trimPhoneNumberString() -\> String"  
> 3 "trimNewLine() -\> String"  
> 3 "trimForNewLineCharacterSet() -\> String"  
> 2 "trimmedRight(characterSet set: NSCharacterSet = default) -\> String"  
> 2 "trimmedLeft(characterSet set: NSCharacterSet = default) -\> String"  
> 1 "trimmingWhitespacesAndNewlines() -\> String"  
> 1 "trimmedStart(characterSet set: CharacterSet = default) -\> String"  
> 1 "trimmedRight() -\> String"  
> 1 "trimmedLeft() -\> String"  
> 1 "trimmedEnd(characterSet set: CharacterSet = default) -\> String"  
> 1 "trimWhitespace() -\> String"  
> 1 "trimPrefix(prefix: String)"  
> 1 "trimInside() -\> String"  
> 1 "trimDuplicates() -\> String"  
> 1 "trim(trim: String) -\> String"  
> 1 "trim(\_ characters: String) -\> String"  
> 1 "trim(\_ characterSet: CharacterSet) -\> \<\>"  
> 1 "stringByTrimmingTailCharactersInSet(\_ set: CharacterSet) -\> String"  
> 1 "sk4TrimSpaceNL() -\> String"  
> 1 "sk4TrimSpace() -\> String"  
> 1 "sk4Trim(str: String) -\> String"  
> 1 "sk4Trim(charSet: NSCharacterSet) -\> String"  
> 1 "prefixTrimmed(prefix: String) -\> String"  
> 1 "omTrim()"  
> 1 "m\_trimmed() -\> String"  
> 1 "m\_trim()"  
> 1 "jjs\_trimWhitespaceAndNewline() -\> String"  
> 1 "jjs\_trimWhitespace() -\> String"  
> 1 "jjs\_trimNewline() -\> String"  
> 1 "jjs\_emptyOrStringAndTrim(str: String?) -\> String"  
> 1 "hyb\_trimRight(trimNewline: Bool = default) -\> String"  
> 1 "hyb\_trimLeft(trimNewline: Bool = default) -\> String"  
> 1 "hyb\_trim(trimNewline: Bool = default) -\> String"

I then looked at implementations just for the #1 form, `trim() -> String`:

> 8 return self.trimmingCharacters(in: NSCharacterSet.whitespaces)  
> 6 return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())  
> 5 return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)  
> 5 return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())  
> 3 return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)  
> 3 return stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())  
> 3 return self.stringByTrimmingCharactersInSet(.whitespaceCharacterSet())  
> 3 return self.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())  
> 2 return trimmingCharacters(in: CharacterSet.whitespaces)  
> 2 return self.trimmingCharacters(in: CharacterSet.whitespaces)  
> 2 return self.trimmingCharacters(in: .whitespacesAndNewlines)  
> 2 return self.trimmingCharacters(in: .whitespaces)  
> 1 return trimmingCharacters(in: .whitespacesAndNewlines)  
> 1 return trimmed  
> 1 return stringByReplacingOccurrencesOfString(" ", withString: "")  
> 1 return strTrimmed  
> 1 return self.trimmingCharacters(in: characterSet)  
> 1 return self.trimmingCharacters(in: NSMutableCharacterSet.whitespaceAndNewline() as CharacterSet)  
> 1 return self.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)  
> 1 return self.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines())  
> 1 return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).trimmingCharacters(in: CharacterSet.whitespaces).trimmingCharacters(in: CharacterSet.whitespaces)  
> 1 return (self as String).trimmingCharacters(in: CharacterSet.whitespaces)  
> 1 return (self as NSString).trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) as String

This tells me that lots of people are solving the same problem the same way. But not all–there are enough differences here to warrant concern that behaviors could diverge now, or later if things like `NSCharacterSet.whitespaceCharacterSet()` and `(NSCharacterSet.whitespaceAndNewlineCharacterSet()` one day diverge for whatever reason (maybe not those specifically, but this is the general idea of seemingly-same-yet-different functions; and the more complicated the problem, the higher the impact of this divergence).

**Conclusion**

Lots of extensions out there solve the same problems, albeit with different implementations. We could think of those implementations on spectra of stability, usability, and others. It might then stand to reason that those most common tasks with the best implementations should be incorporated into the Swift stdlib (maybe with even better implementations).

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [May 16, 2018, 7:26pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/2 "2018-05-16T19:26:46Z")

</div>

This is awesome stuff. Do you have the top 3-10 method-families for any of these extensions?

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [May 16, 2018, 7:47pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/3 "2018-05-16T19:47:50Z")

</div>

Wow, this is a really interesting way to do this analysis. Thank you for doing this! We clearly need a pitch for a trimming API :-)

---

<div class="post-metadata">

**Author:** ![armcknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/armcknight/32/3486_2.png) [@armcknight](https://forums.swift.org/u/armcknight)\
**Post date:** [May 16, 2018, 8:55pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/4 "2018-05-16T20:55:15Z")

</div>

This is fun!

Here are the top function families (ps thanks for that bit of vocab!) for the top 4 extended APIs:

- top 4 families for String: trim, substring, email, height (for layout)
- String conversions–after whittling away those top 4 families, I noticed lots of "toBool" etc functions
- top 4 Array: shuffle, random, pop/push, shift
- top 3 Date: today, format, offset (various calculations for elapsed time in/between different units)
- top 4 Int: random, digits, times/upto/downto (looping), digits, bytes

The complete lists are currently here so you can see the breakdown of declarations: [colloquial-swift/function\_families at master · armcknight/colloquial-swift · GitHub](https://github.com/armcknight/colloquial-swift/tree/master/function_families)

---

<div class="post-metadata">

**Author:** ![Panajev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/panajev/32/1227_2.png) [@Panajev](https://forums.swift.org/u/Panajev)\
**Post date:** [May 16, 2018, 9:08pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/5 "2018-05-16T21:08:12Z")

</div>

Awesome useful work!

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [May 16, 2018, 10:57pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/6 "2018-05-16T22:57:18Z")

</div>

> [@armcknight](#):
>
> - top 4 families for String: trim, substring, email, height (for layout)
> - String conversions–after whittling away those top 4 families, I noticed lots of "toBool" etc functions
> - top 4 Array: shuffle, random, pop/push, shift
> - top 3 Date: today, format, offset (various calculations for elapsed time in/between different units)
> - top 4 Int: random, digits, times/upto/downto (looping), digits, bytes

- Trim seems like a win.
- Random (Int and Array): [SE-0202](https://github.com/apple/swift-evolution/blob/master/proposals/0202-random-unification.md)
- Array has popLast, but queue functions often come up in discussions and wish lists.
- Date really needs @DaveDeLong's input.
- Times, up to and down to, are somewhat covered by [cycles, batches, and concatenation](https://gist.github.com/erica/6af2e5d6382d814f39b936333fc0a016)
- Digits might have some opportunity (along with radix) although there's already some good stuff in stdlib.
- Not quite sure what the "bytes" covers? You mean like 4 sequential bytes (0x00, 0xFF, 0x00, 0x23) from an Int regardless of endianness?

Also, did you see much about left and right padding? I remember @beccadax pitching some formatting stuff a while back.

---

<div class="post-metadata">

**Author:** ![Nicole\_Jacque](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicole_jacque/32/245_2.png) [@Nicole\_Jacque](https://forums.swift.org/u/Nicole_Jacque)\
**Post date:** [May 16, 2018, 11:56pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/7 "2018-05-16T23:56:27Z")

</div>

Listed on May 16, 2018, 11:56pm UTC



---

<div class="post-metadata">

**Author:** ![tkremenek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkremenek/32/9115_2.png) [@tkremenek](https://forums.swift.org/u/tkremenek)\
**Post date:** [May 17, 2018, 1:13am UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/8 "2018-05-17T01:13:50Z")

</div>

I think this is fantastic. I really appreciate the data-oriented approach!

One thought about this:

> [@armcknight](#):
>
> search github for Swift repositories matching certain criteria (e.g., name contains "util" or "extension"): 5,810 unique repositories currently returned

I think it would _also_ be interesting to relax the criteria, and look at GitHub projects containing apps, etc. The difference with those projects is that these will be codebases that implement some larger feature or purpose, and the extensions you will find are the things that were _needed_ to get core work done. There may be some selection bias here with looking at the repositories that match the restricted criteria because the whole purpose of those repositories might be to vend utility APIs. That doesn't mean the signal here is wrong, but there may be some over-counting and thus over-weighing of the relative importance of some of these.

---

<div class="post-metadata">

**Author:** ![davedelong](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davedelong/32/38708_2.png) [@davedelong](https://forums.swift.org/u/davedelong)\
**Post date:** [May 17, 2018, 6:01pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/9 "2018-05-17T18:01:51Z")

</div>

> [@Erica\_Sadun](#):
>
> Date really needs @DaveDeLong's input.

+1

---

<div class="post-metadata">

**Author:** ![Tony\_Parker](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tony_parker/32/273_2.png) [@Tony\_Parker](https://forums.swift.org/u/Tony_Parker)\
**Post date:** [May 17, 2018, 7:41pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/10 "2018-05-17T19:41:33Z")

</div>

With respect to `Date`, `today` is a bit of a misnomer since `Date` is a moment in time and not a range. That said, we do have `Date.now`. It also has the usual mathematical operations for calculations about absolute time (`+`, `-`, etc).

---

<div class="post-metadata">

**Author:** ![Tony\_Parker](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tony_parker/32/273_2.png) [@Tony\_Parker](https://forums.swift.org/u/Tony_Parker)\
**Post date:** [May 17, 2018, 7:54pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/11 "2018-05-17T19:54:32Z")

</div>

Oh, I guess I misremembered about `Date.now` (which we could probably add), but it is the same as `Date()`.

---

<div class="post-metadata">

**Author:** ![armcknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/armcknight/32/3486_2.png) [@armcknight](https://forums.swift.org/u/armcknight)\
**Post date:** [May 17, 2018, 9:21pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/12 "2018-05-17T21:21:44Z")

</div>

Thanks! I agree, there's no reason to exclude extension/other declarations from apps and other codebases. I think I went too far down the "utility lib" route, before settling on extension declarations, and didn't look back to see what I'd missed :)

I'll tweak a few filters and rerun the analysis to see how it changes the numbers, I'll check back maybe Monday or Tuesday with some results. My guess is that like you said, this is going to extend the long tails of the distributions by adding more uniquely specialized functions, even if it also notches up a few more `String.trim()`s 😉

I haven't figured out good questions to ask about those long tails yet, or good strategies for automated analysis. I was thinking of tokenizing the identifiers and doing some clustering analysis on the words this extracts.

---

<div class="post-metadata">

**Author:** ![davedelong](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davedelong/32/38708_2.png) [@davedelong](https://forums.swift.org/u/davedelong)\
**Post date:** [May 17, 2018, 9:47pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/13 "2018-05-17T21:47:44Z")

</div>

This may not be the right thread for it, but IMO we shouldn't encourage `Date` usage, since it is fundamentally the wrong way to express calendrical ideas (calendar values are ranges, not instants).

If we do want some sort of "now()" concept, we should consider adding a `Clock` API. I've got that built in to [Chronology](https://github.com/davedelong/chronology) and it is _awesome_.

---

<div class="post-metadata">

**Author:** ![Tony\_Parker](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tony_parker/32/273_2.png) [@Tony\_Parker](https://forums.swift.org/u/Tony_Parker)\
**Post date:** [May 17, 2018, 9:54pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/14 "2018-05-17T21:54:50Z")

</div>

I disagree that Date is fundamentally broken, but we can start a new thread about it if you like.

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [May 18, 2018, 4:22am UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/15 "2018-05-18T04:22:24Z")

</div>

Dave didn't say it was broken. I read it as `Date` is misused. I would love a new thread for this topic. Date and time handling in Foundation (essentially inherited by Swift) is powerful, but it has some blind spots with regards to ergonomics.

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [May 18, 2018, 9:36pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/16 "2018-05-18T21:36:20Z")

</div>

> [@Chris\_Lattner3](#):
>
> Wow, this is a really interesting way to do this analysis. Thank you for doing this! We clearly need a pitch for a trimming API :-)

Ask and receive. (cc @armcknight )

> <https://gist.github.com/erica/e635a86405bda185d80f696949a4272d>

---

<div class="post-metadata">

**Author:** ![tkrajacic](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkrajacic/32/1173_2.png) [@tkrajacic](https://forums.swift.org/u/tkrajacic)\
**Post date:** [May 18, 2018, 10:08pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/17 "2018-05-18T22:08:41Z")

</div>

I guess `public func trimmed() -> String` should not be mutating?

---

<div class="post-metadata">

**Author:** ![Dante-Broggi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dante-broggi/32/7955_2.png) [@Dante-Broggi](https://forums.swift.org/u/Dante-Broggi)\
**Post date:** [May 18, 2018, 10:28pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/18 "2018-05-18T22:28:14Z")

</div>

I think this should wait for (and use) the Character Properties pitch, and their definition for whitespace characters (of which they propose 2 options for debate):

> [@Pitch: Character and String properties](https://forums.swift.org/t/pitch-character-and-string-properties/11620):
>
> (This has been updated with the most recent pitch) Gist-formatting version: [Pitch: Character and String properties · GitHub](https://gist.github.com/milseman/c8c50ed0eef0a21181b0d4eeedbad819)Pitch: Character and String properties Authors: [Michael Ilseman](https://github.com/milseman), [Tony Allevato](https://github.com/allevato) A PR with the implementation can be found [here](https://github.com/apple/swift/pull/15880)Introduction@allevato (a co-author here) started at pitch at [Adding Unicode properties to UnicodeScalar/Character - Pitches - Swift Forums](https://forums.swift.org/t/adding-unicode-properties-to-unicodescalar-character/9310/50), which exposes Unicode properties from the [Unicode Character Database](http://unicode.org/reports/tr44/). These are Unicode expert/enthusia…

---

<div class="post-metadata">

**Author:** ![Michael\_Ilseman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_ilseman/32/44640_2.png) [@Michael\_Ilseman](https://forums.swift.org/u/Michael_Ilseman)\
**Post date:** [May 18, 2018, 10:33pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/19 "2018-05-18T22:33:04Z")

</div>

I think that long-term, we will definitely want some kind of whitespace control / formatting / document-building solution.

In the near(er) term, we should deliver trim/pad/center/etc., which should compose well with a `String.lines` property (which we should also do soon). I was planning on tackling these after wrapping up more ABI-critical work, but if anyone else is interested in exploring this, let's spin off a thread and collaborate!

---

<div class="post-metadata">

**Author:** ![pvieito](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pvieito/32/33613_2.png) [@pvieito](https://forums.swift.org/u/pvieito)\
**Post date:** [May 18, 2018, 10:43pm UTC](https://forums.swift.org/t/surveying-how-swift-evolves/12726/20 "2018-05-18T22:43:05Z")

</div>

Yes, `Date.now` is really needed for clarity. I remember trying `Date(timeIntervalSinceNow: 0)` before realizing there had to be a simpler way.

Could this API be added through Swift Evolution?

[Next page](https://forums.swift.org/t/surveying-how-swift-evolves/12726.md?page=2)
