<div dir="ltr"><div>Namespaces</div><div>Author(s): TJ Usiyan</div><div><br></div><div>Introduction</div><div><br></div><div>A `namespace` keyword for swift hold related global variables.</div><div><br></div><div>Motivation</div><div><br></div><div>We often want to collect related variables which do not, for whatever reason, fit neatly into a type provided by Swift. Importing Objective C &#39;magic strings&#39; as Joshua Sullivan does [here] is one such example. The solution he arrives at is a `struct` type with many type variables and no fields.</div><div><span class="" style="white-space:pre">        </span></div><div><span class="" style="white-space:pre">        </span>struct NotificationNames {</div><div><span class="" style="white-space:pre">        </span>  static let userDataChanged = &quot;UserDataChangedNotificationName&quot;</div><div><span class="" style="white-space:pre">        </span>  static let receivedAlert = &quot;ReceivedAlertNotificationName&quot;</div><div><span class="" style="white-space:pre">        </span>  static let peanutButterJellyTime = &quot;ItsPeanutButterJellyTimeNotificationName&quot;</div><div><span class="" style="white-space:pre">        </span>}</div><div><br></div><div>Users of this API are not meant to create instances of this type, yet it is still possible. A solution to this is to use an `enum` without cases. </div><div><br></div><div><span class="" style="white-space:pre">        </span>enum NotificationNames {</div><div><span class="" style="white-space:pre">        </span>  static let userDataChanged = &quot;UserDataChangedNotificationName&quot;</div><div><span class="" style="white-space:pre">        </span>  static let receivedAlert = &quot;ReceivedAlertNotificationName&quot;</div><div><span class="" style="white-space:pre">        </span>  static let peanutButterJellyTime = &quot;ItsPeanutButterJellyTimeNotificationName&quot;</div><div><span class="" style="white-space:pre">        </span>}</div><div><br></div><div>No instances of the `enum` can be made. That this inability to create an instance is intentional is only conveyed via the type system. </div><div><br></div><div>Proposed solution</div><div><br></div><div>Clarity would be greatly improved if we could use `namespace` as a synonym for an enum with no cases. This would allow us to avoid repetition of `static` as well.</div><div><br></div><div><span class="" style="white-space:pre">        </span>namespace NotificationNames {</div><div><span class="" style="white-space:pre">        </span>  let userDataChanged = &quot;UserDataChangedNotificationName&quot;</div><div><span class="" style="white-space:pre">        </span>  let receivedAlert = &quot;ReceivedAlertNotificationName&quot;</div><div><span class="" style="white-space:pre">        </span>  let peanutButterJellyTime = &quot;ItsPeanutButterJellyTimeNotificationName&quot;</div><div><span class="" style="white-space:pre">        </span>}</div><div><br></div><div>Detailed design</div><div><br></div><div>Namespaces can be represented in the type system as enums. All variables declared in a namespace&#39;s scope would be type variables.</div><div><br></div><div>Impact on existing code</div><div><br></div><div>This is an additive change and, as such, should not break any existing code. Though it doesn&#39;t matter, this addition could possibly attain ABI compatibility if namespaces are represented as enums without cases.</div><div><br></div><div>Alternatives considered</div><div><br></div><div>Don&#39;t implement namespaces. Developers could continue(begin?) using an empty enum to hold type variables.</div></div>