# \`namespace\` keyword

**URL:** <https://forums.swift.org/t/namespace-keyword/11338>\
**Category:** Pitches\
**Created:** [March 24, 2018, 8:43pm UTC](https://forums.swift.org/t/namespace-keyword/11338 "2018-03-24T20:43:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmartinelli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vmartinelli/32/8443_2.png) [@vmartinelli](https://forums.swift.org/u/vmartinelli)\
**Post date:** [March 24, 2018, 8:43pm UTC](https://forums.swift.org/t/namespace-keyword/11338/1 "2018-03-24T20:43:20Z")

</div>

I'm still using things like: `public /* namespace */ enum MyNamespace { ... }` to fake namespaces. Any news about `namespace` keyword or its possibility of being implemented?

---

<div class="post-metadata">

**Author:** ![Tino](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tino/32/1073_2.png) [@Tino](https://forums.swift.org/u/Tino)\
**Post date:** [March 24, 2018, 8:46pm UTC](https://forums.swift.org/t/namespace-keyword/11338/2 "2018-03-24T20:46:25Z")

</div>

Fake namespaces are discussed to be used in the stdlib

> [@Introducing Namespacing for Common Swift Error Scenarios](https://forums.swift.org/t/introducing-namespacing-for-common-swift-error-scenarios/10773/21):
>
> I think that’s exactly what I’m trying to get at is that the semantics of what these should look like is up to the team deciding to adopt them. They might want them to crash in any build type and another team might want some to crash in Debug but not release.

---

<div class="post-metadata">

**Author:** ![vmartinelli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vmartinelli/32/8443_2.png) [@vmartinelli](https://forums.swift.org/u/vmartinelli)\
**Post date:** [March 24, 2018, 10:30pm UTC](https://forums.swift.org/t/namespace-keyword/11338/3 "2018-03-24T22:30:25Z")

</div>

Thank you for the link. But I'm thinking about other scenarios too. Instead of writing this:

```
public enum SomeCoolService { }

extension SomeCoolService {
    
   public class API {
       //...
   }

}

```

To accomplish this:

`let api = SomeCoolService.API()`

I'd like to write this:

```
public namespace MyCoolService {

    public class API {
        // ...
    }

}

```

Or:

```
public namespace MyCoolService { }

extension MyCoolService { // this as a real namespace, not a enum

    public class API { ... }

}

```

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hlovatt/32/3044_2.png) [@hlovatt](https://forums.swift.org/u/hlovatt)\
**Post date:** [March 25, 2018, 1:19am UTC](https://forums.swift.org/t/namespace-keyword/11338/4 "2018-03-25T01:19:48Z")

</div>

I would like `namespace` since I find `enum` counterintuitive.

---

<div class="post-metadata">

**Author:** ![timv](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/timv/32/13304_2.png) [@timv](https://forums.swift.org/u/timv)\
**Post date:** [March 25, 2018, 6:05am UTC](https://forums.swift.org/t/namespace-keyword/11338/5 "2018-03-25T06:05:10Z")

</div>

An additional benefit of a `namespace` keyword is that functions and variables are `static` by default, because a namespace can't be instantiated:

```swift
// current syntax
enum Foo {
    static var bar: Bar { ... }
    static func baz() { ... }
}

// proposed syntax
namespace Foo {
    var bar: Bar { ... }
    func baz() { ... }
}

```

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [March 25, 2018, 8:47am UTC](https://forums.swift.org/t/namespace-keyword/11338/6 "2018-03-25T08:47:01Z")

</div>

I wonder if the functionality of a 'namespace' is tied together with the `namespace` keyword in a programming language. I don't like fake namespaces in Swift. Even the WWDC14 presentation had a slide live on stage that said the new programming language Swift included namespaces, but where are they?

Btw. if you nest your types you will probably hit this at some point:

[https://bugs.swift.org/browse/SR-631](https://bugs.swift.org/browse/SR-631) (The most voted, not ABI related - therefore low priority, issue on JIRA.)

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [March 25, 2018, 12:54pm UTC](https://forums.swift.org/t/namespace-keyword/11338/7 "2018-03-25T12:54:33Z")

</div>

I would much rather see sub modules than namespaces and I’m skeptical that it would be a good idea to have both. This makes me very reluctant to see namespaces introduced. I think we need to resolve submodules first and if people still feel namespaces would add something we can revisit the topic in the future.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [March 25, 2018, 1:00pm UTC](https://forums.swift.org/t/namespace-keyword/11338/8 "2018-03-25T13:00:41Z")

</div>

No objections at all, I also would love to see submodules happen. While writing my previous answer I totally forget about them, glad you remind me about them.

---

<div class="post-metadata">

**Author:** ![vmartinelli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vmartinelli/32/8443_2.png) [@vmartinelli](https://forums.swift.org/u/vmartinelli)\
**Post date:** [March 25, 2018, 5:33pm UTC](https://forums.swift.org/t/namespace-keyword/11338/9 "2018-03-25T17:33:06Z")

</div>

Good point about SR-631. Yep. I had this problem and I had to rethink the architecture I was creating in that occasion.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [March 25, 2018, 5:36pm UTC](https://forums.swift.org/t/namespace-keyword/11338/10 "2018-03-25T17:36:09Z")

</div>

Well the issue is solvable without restructuring the project, but the solution isn't convenient. You have to shuffle the files in Xcode's compile sources until Xcode is satisfied.

---

<div class="post-metadata">

**Author:** ![vmartinelli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vmartinelli/32/8443_2.png) [@vmartinelli](https://forums.swift.org/u/vmartinelli)\
**Post date:** [March 25, 2018, 5:43pm UTC](https://forums.swift.org/t/namespace-keyword/11338/11 "2018-03-25T17:43:54Z")

</div>

What's the difference between submodules and namespaces?

I took the word `namespace` because I think it is a more intuitive word. In other worlds (like .NET) there is `assembly` (something like our Xcode framework/Swift module) and `namespace` (something like our... no... wait... something like our empty `enum`).

Do submodules are other concept or simply other name for the namespaces?

I'm asking this because creating other framework projects in Xcode only to have submodules or namespaces will be... Much ado about almost nothing.

I think a `namespace` with the behaviour of a empty `enum` + static vars/funcs by default is simple and not hard to implement/learn/use.

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [March 26, 2018, 3:57pm UTC](https://forums.swift.org/t/namespace-keyword/11338/12 "2018-03-26T15:57:01Z")

</div>

Submodules are a pretty deep topic and broad design space. They were discussed quite heavily last year, including the relationship to namespaces. You can find the previous threads by searching the forums: [Search results for 'submodules category:18' - Swift Forums](https://forums.swift.org/search?q=submodules%20category%3A18).

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 26, 2018, 4:16pm UTC](https://forums.swift.org/t/namespace-keyword/11338/13 "2018-03-26T16:16:09Z")

</div>

i don’t get how this adds anything that enum can’t already do. you’re just adding another keyword to the language

---

<div class="post-metadata">

**Author:** ![vmartinelli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vmartinelli/32/8443_2.png) [@vmartinelli](https://forums.swift.org/u/vmartinelli)\
**Post date:** [March 26, 2018, 5:08pm UTC](https://forums.swift.org/t/namespace-keyword/11338/14 "2018-03-26T17:08:04Z")

</div>

@taylorswift The problem using `enum` for this is that semantically its purpose is to be an enumeration and not to fake namespaces. How one explain to other learning the language that in Swift we use `enum` this way? Also this collateral namespace feature of `enum` could be removed from the language anytime (and, IMHO, should be removed when `namespace` is implemented).

---

<div class="post-metadata">

**Author:** ![vmartinelli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vmartinelli/32/8443_2.png) [@vmartinelli](https://forums.swift.org/u/vmartinelli)\
**Post date:** [March 26, 2018, 5:10pm UTC](https://forums.swift.org/t/namespace-keyword/11338/15 "2018-03-26T17:10:00Z")

</div>

@anandabits Thank you for the links. Well... But I stil think that namespace could be one thing and submodule other thing.

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [March 26, 2018, 5:11pm UTC](https://forums.swift.org/t/namespace-keyword/11338/16 "2018-03-26T17:11:45Z")

</div>

@vmartinelli it's possible but I would prefer to wait on namespaces until we have thoroughly vetted the submodule topic and know where that lands. That's not going to happen in the Swift 5 timeframe.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 26, 2018, 5:36pm UTC](https://forums.swift.org/t/namespace-keyword/11338/17 "2018-03-26T17:36:29Z")

</div>

i mean when i was learning the language using enum made perfect sense. types in Swift double as namespaces, and out of the 3 types of types, class, struct, and enum, enum is the most “lightweight” so it makes sense it would be the preferred container if all you wanted was a namespace and nothing else

also in general i’m just against adding more global keywords to the language that serve no purpose

---

<div class="post-metadata">

**Author:** ![masters3d](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@masters3d](https://forums.swift.org/u/masters3d)\
**Post date:** [March 26, 2018, 5:36pm UTC](https://forums.swift.org/t/namespace-keyword/11338/18 "2018-03-26T17:36:44Z")

</div>

I just saw a [talk](https://tsconf.io/presenters.html) (i dont think the keynote is online yet) with the lead developer of typescript and he said that with modules, namespaces have less meaning [https://www.typescriptlang.org/docs/handbook/namespaces.html](https://www.typescriptlang.org/docs/handbook/namespaces.html)

I think I much rather have better module support in Swift than namespaces.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 26, 2018, 5:38pm UTC](https://forums.swift.org/t/namespace-keyword/11338/19 "2018-03-26T17:38:58Z")

</div>

idk what modules are in typescript but Swift’s concept of a “module” is really more in terms of shipping than grouping related functionality. a Swift module is basically what you would call a package in other languages and so they have a concept of versioning, tagging, and ABI boundaries that make no sense to talk about when you’re talking about namespaces

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [March 26, 2018, 5:44pm UTC](https://forums.swift.org/t/namespace-keyword/11338/20 "2018-03-26T17:44:35Z")

</div>

The primary goal of submodules (IMO) is to separate the _logical_ structure and encapsulation afforded by modules from the _physical_ structure (and associated overhead) of a build product.

[Next page](https://forums.swift.org/t/namespace-keyword/11338.md?page=2)
