Swift Namespaces

I've seen some people complaining about the lack of namespaces in Swift… well, I'm not sure if I've seen this anywhere else, but you can fake namespaces if you really must.

enum std {
    static func add(x: Int, _ y: Int) -> Int { return x + y }

    enum more {
        static func sub(x: Int, _ y: Int) -> Int { return x - y }

    struct apples {
        var count: Int;

std.add(1, 2)
std.more.sub(1, 2)

var apples = std.apples(count: 12)
apples.count = 10

Now, I don't really recommend this, but it's kind of fun to hack around sometimes.


Oh… and we can mimic using too:

typealias m = std.more
m.sub(1, 2)

I think you still need to have it qualified though, so no naked use of sub.

Swift Namespaces