Swift Standard Library Type Graph

Type graph of the Swift standard library
Type graph of the Swift standard library

Quick weekend hack: A type graph of the Swift (4.2) standard library, containing every single type and how they connect to other types.

I had this idea when browsing Apple’s Swift documentation: At the bottom of the page for a given type, there’s a Relationships section. It lists which types the current one conforms to, inherits from, or is being conformed to.

While interesting, it can be hard to discover type relationships this way. See Collection for an example. I wanted to see a bigger picture. What kind of relationships would emerge if we were to combine all those connections into a single graph?

Some of the relationships of Collection
Some of the relationships of Collection

Type Graph

You can see the resulting type grap above. Click for full size.

Looking at it, we see an abundance of protocols and structs. Swift has always been described as protocol-oriented (most famously in the Crusty talk at WWDC 2015) and the graph supports that notion.

Surprisingly, there are only 6 classes in the entire standard library, everything else are value-types. For comparison, I generated a graph for the Foundation framework and it’s the complete opposite there. Everything is a class.

There are a handful of clusters around protocols. Most obvious for base types like Sequence/Collection and Equatable/Hashable. But there are also notable clusters for Swift’s String and numeric types.

The graph also shows the relative complexity of the numeric types:

If you haven’t worked with integers much, you probably have never looked at these protocols in detail. There are many such barely visible protocols in the standard library.

It’d be interesting to see how the graph evolves with future Swift versions.

How I Made It

I created a script to create the type graph for any Swift version. Check it out on GitHub alongside detailed instructions.

The data is generated directly from the Swift standard library source code. Before it can be used, the Swift source code needs preprocessing with the gyb preprocessor. I parsed the generated code with sourcekitten, a fantastic command-line tool to call into SourceKit. From there, I extract all public Swift types and create the type relationship graph. The graph is then rendered into a PDF/SVG with graphviz.