Naming your Swift Packages

So, in cleaning up a little of my massive backlog of Swift code packs, I decided to take advantage of the Swift Package Manager and put a few things up on github. Which I did.

It was only after that that I realized what a total mess I made of things by naming them in what I perceived at the time to be a logical, sensible way.

Package names need to be clear and specific, yes, but they should avoid terms that will overlap because when you have a package called SwiftString and every Bob, Jane, and Harry also has a package called SwiftString, name collisions are inevitable.

Your SwiftString.doSomethingMeaningfulWithAString() is indistinguishable from everyone else’s SwiftString.doSomethingMeaningfulWithAString(). At this time there is no built-in way that I know of how to deal with module name collisions.

Future solutions might include some way to introduce further namespacing. For example, the Package declaration syntax might add an origin:

import PackageDescription

let package = Package(
    name: "SwiftString"
    origin: "com.sadun"
)

With this, you could avoid name conflicts with

import com.sadun.SwiftString
import com.LeeJason.SwiftString
and
com.sadun.SwiftString.doSomethingMeaningfulWithAString()

For obvious reasons, this could get a little annoying, although if you only import one of the two SwiftString packages, you wouldn’t have to go nearly that far with the disambiguation.

At this time, there aren’t public, blessed, or official repositories of SPM packages but you know this is going to self-organize fairly soon if history is an example. Making sure there’s a clear way to use multiple, potentially overlapping packages will be a good thing.

And, until then, prefer SadunSwiftString to SwiftString and avoid the issue from the start.

3 Comments

  • To that end, I wish there was an optional way to rename imports.

    “`
    import com.sadun.SwiftString as Stringy

    Stringy.doSomethingMeaningfulWithAString()
    “`

  • Better would be to have import syntax that lets you alias conflicting symbols like most other languages do:

    from Foundation import NSString as AppleString

  • Thats the reason I like about Obj-C prefixes. When reading code outside of IDE it is easy to keep track where each class is coming from. NS, UI, etc is Apple . some random 3 letters comes from third party, etc