[swift-evolution] Proposal: Namespaces

David Owens II david at owensd.io
Thu Jan 14 13:34:11 CST 2016


What’s the status on this? Is someone willing to write a proper proposal for this?

I just ran into to this yesterday. I have a project has multiple parsers and naturally, there is a need for multiple `Tokenizer` classes. It’s getting a big obnoxious to continue abuse struct/enum for this.

-David


> On Dec 13, 2015, at 5:31 PM, Cole Kurkowski via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 100% agree that using a struct this way is far from ideal. I just don't think the problem here is realistically that big of a deal. 
> 
> If there were major performance gains to be had from the compiler knowing a data structure was essentially a set of grouped constants, then I would completely support adding a keyword to indicate such. I don't believe that's the case though, as it stands I believe both your proposal and my alternative are just syntactic sugar with the potential for confusion.
> 
> On Dec 13, 2015, at 18:16, T.J. Usiyan <griotspeak at gmail.com <mailto:griotspeak at gmail.com>> wrote:
> 
>> The use case described in the proposal and your response doesn't use structs as a "bucket of values". I uses *the type* as a bucket of values. The point of using an empty enum is to emphasize this point. The overal point of this proposal is to introduce semantics that obviate the need to 'decide' between enum and struct since neither are the type actually desired. I suggest using an enum as the implementation with the hope that it will simplify implementation.
>> 
>> Your suggestion of `static struct` emphasizes the utility of a namespace keyword. Is `static struct …` a declaration of a type or an instance and of what? 
>> 
>> TJ
>> 
>> On Sun, Dec 13, 2015 at 6:44 PM, Cole Kurkowski <crk at fastmail.com <mailto:crk at fastmail.com>> wrote:
>> IMO using  an enum is non-intuitive. A struct is a "bucket of values" and so makes fits with the usage here. If the ability to instantiate the NotificationNames struct is a problem, adding a 
>> 
>> private init() 
>> {
>> 
>> }
>> 
>> seems more intuitive than co-opting an enum, which has a fairly established use case. 
>> 
>> Your proposal would essentially be adding a keyword that is transformed back into "enum" during compilation. I don't think that a relatively minor improvement in clarity for a fairly small use case is worth adding a language feature.
>> 
>> Allowing structs to be declared as static would be another way of simplifying this kind of declaration, i.e.
>> 
>> static struct NotificationNames {
>>      let userDataChanged = ...
>>      let recievedAlert = ...
>> }
>> 
>> This would allow dropping the private init declaration and dropping the static from each property. I'm not sure I think this shortcut is necessary or even if it's a good idea, but I do think it fits with the language a little better than your current proposal. 
>> 
>> On Dec 11, 2015, at 04:39, T.J. Usiyan via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>>> Namespaces
>>> Author(s): TJ Usiyan
>>> 
>>> Introduction
>>> 
>>> A `namespace` keyword for swift hold related global variables.
>>> 
>>> Motivation
>>> 
>>> We often want to collect related variables which do not, for whatever reason, fit neatly into a type provided by Swift. Importing Objective C 'magic strings' 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.
>>> 	
>>> 	struct NotificationNames {
>>> 	  static let userDataChanged = "UserDataChangedNotificationName"
>>> 	  static let receivedAlert = "ReceivedAlertNotificationName"
>>> 	  static let peanutButterJellyTime = "ItsPeanutButterJellyTimeNotificationName"
>>> 	}
>>> 
>>> 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. 
>>> 
>>> 	enum NotificationNames {
>>> 	  static let userDataChanged = "UserDataChangedNotificationName"
>>> 	  static let receivedAlert = "ReceivedAlertNotificationName"
>>> 	  static let peanutButterJellyTime = "ItsPeanutButterJellyTimeNotificationName"
>>> 	}
>>> 
>>> No instances of the `enum` can be made. That this inability to create an instance is intentional is only conveyed via the type system. 
>>> 
>>> Proposed solution
>>> 
>>> 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.
>>> 
>>> 	namespace NotificationNames {
>>> 	  let userDataChanged = "UserDataChangedNotificationName"
>>> 	  let receivedAlert = "ReceivedAlertNotificationName"
>>> 	  let peanutButterJellyTime = "ItsPeanutButterJellyTimeNotificationName"
>>> 	}
>>> 
>>> Detailed design
>>> 
>>> Namespaces can be represented in the type system as enums. All variables declared in a namespace's scope would be type variables.
>>> 
>>> Impact on existing code
>>> 
>>> This is an additive change and, as such, should not break any existing code. Though it doesn't matter, this addition could possibly attain ABI compatibility if namespaces are represented as enums without cases.
>>> 
>>> Alternatives considered
>>> 
>>> Don't implement namespaces. Developers could continue(begin?) using an empty enum to hold type variables.
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160114/5ed90434/attachment.html>


More information about the swift-evolution mailing list