[swift-evolution] Dropping NS Prefix in Foundation
jhull at gbis.com
Sat May 7 20:16:41 CDT 2016
> On May 7, 2016, at 9:53 AM, Tony Parker <anthony.parker at apple.com> wrote:
>> On May 7, 2016, at 6:06 AM, Jonathan Hull <jhull at gbis.com <mailto:jhull at gbis.com>> wrote:
>> -1 on this as well. How much does dropping NS really help things anyway?
>> All it does is force everyone to learn which things still have NS and which don’t. It also makes things much more difficult to search for… searching for NS_ gives the results you want quickly vs searching for anything in Swift foundation (e.g. Array -- which gives you a mixture of other programming languages and Taylor Swift gossip).
>> My proposal would be to keep NS for everything and then slowing making versions without the prefix, either by rewriting them to be better in Swift or simply aliasing the NS version. Once you have critical mass for useful things (around Swift 5~6), you can separate all the NSStuff out into their own NSFoundation which would be used only for backwards compatibility.
>> To the inevitable question: Wont having NS and Non-NS versions be confusing (especially if some are just aliases)? My answer is that it is less confusing than this proposal. There is a simple rule: Things without NS are always the new and preferred methods. Things with NS are there for compatibility and will continue to work the way they do in ObjectiveC (even if you have to import “NSFoundation” to get them instead of just “Foundation")
> I think that this approach ends up with confusion as well. Maybe we end up with UserDefaults and NSUserDefaults? One is written in Swift and other is not, but what difference would that make to the caller? Swift itself is not written completely in Swift. Instead, let’s have one UserDefaults that has an API appropriate for Swift. You’ve seen some of that already with changes to the names of its methods (https://github.com/apple/swift/blob/master/apinotes/Foundation.apinotes#L575 <https://github.com/apple/swift/blob/master/apinotes/Foundation.apinotes#L575> as one example, which takes advantage of overloading to simplify the API).
I think my main point here is that just because we are making breaking changes in Swift 3, it doesn’t mean we have to do everything as a breaking change. I feel like we are being hasty in trying to bring everything over so quickly without thought, when it could be done gradually over 3-4 years… making large improvements as we pull things over. If we do this now, we will be locking ourselves into a base design that will always be between two worlds, and thus not really ideal for either world. Lowest common denominator. We won’t be able to do things like Swift’s String or Array.
In cases like UserDefaults where it makes sense to pull over the design without much modification, you can have a single design and just alias it to “UserDefaults”
There will be confusion with any system, but this at least has a simple clarifying rule. If there is both a NS and Non-NS version, the non-NS version is the new/shiny/preferred one.
What happens if we just bring something like NSURL over and call it URL, but then decide we need a completely different conceptual model (or we want to make it a value type instead of a reference type)? All of the non-breaking fixes I can think of are way more confusing than having separate NSURL and Url types (which mirror the differences of NSArray and Array).
>> Side Note: I would also REALLY like to see a swift native improvement on NSAttributedString with native literal support.
> NSAttributedString was part of the value types proposal at the beginning but deferred for a few practical reasons. One reason is that I want additional existential support in the language first. AttributedString has the concept of a “longest effective run”, which is calculated by checking for equality between attributes. Attributed string should allow for AnyEquatableAndCopyable members of its attribute dictionary. A second problem is that the entire Cocoa text system (and hundreds of higher level APIs) are based on top of the NSString Index concept, which is hidden behind the unicodeScalar view of Swift.String. I would like a more unified story of how these two can interoperate before we revamp the base type.
> Both of these are solvable, but they require time to collaborate between teams to decide what the right platform-wide approach is. We’ll do it, but it will not happen immediately.
I think what I would like to see is a new model which makes editing & creation much less of a pain, but which can be bridged to NSAttributedString so we don’t have to propagate changes (e.g. the indexing model) throughout the system all at once.
My main opposition to bringing everything over right now is that it makes changes like that much more difficult in the future.
>>> There’s no question that we can improve Coding for Swift. I have actually explored this area quite a bit although I don’t have anything planned for Swift 3 at this time.
>>> The general point is though, that we can do it by extending Foundation in that direction over time. In fact, keyed archiving is the perfect example of how we were able to do just that in the past in Objective-C. NSArchiver already existed and served a particular purpose, so we extended the concept into NSKeyedArchiver using the facilities available to us at the time.
>> I would be curious to hear about your explorations (either in another thread or offlist)
>> I have written a couple of experimental versions of an improved Coding system for Swift. The key idea is to use closures to allow coding of arbitrarily complex nested types (e.g an array of tuples of Dictionaries: [(String, [String:Int])] ). It works pretty well, but unfortunately currently taxes the compiler to the point where it randomly crashes during compilation. I am waiting for the new generics stuff to come online before I explore further, since I believe that will dramatically simplify the code.
>> The other idea which I would like to see replicated is that it codes to an intermediate format which can then be transformed to/from binary data, XML, BSON (JSON + some representation for Binary Data) or some other format...
>> It also interoperates very well with existing NSCoding classes, which is an important feature.
> One thing to think about here is what the role of NSCoding is in the first place. It was designed to support archiving of UI objects to nib files. It has been pressed into service for all kinds of other interesting tasks since; UI state restoration, document formats, and IPC wire protocol, to name a few. It may be worthwhile to decide if these are really all the same use case or not. I’m honestly not sure yet. Some of these are very focused on dynamic behavior (that is, the object graph is not known in advance). For some, custom object types are really important (NSXPC takes full advantage of this, and is basically its reason for existence over the raw libxpc API). For others it is not (a raw document file of one million double values). Some of these have different performance requirements than others (archiving fast is important to IPC but unarchiving fast is important to loading nib files).
Makes sense. I haven’t really worried to much about efficiency in my experiments (I am just trying to get it to work at all), but I can see where that will be important for the official version.
I wonder if protocols help us here? They might allow us to swap out implementations and have something optimized for the situation (e.g. your file of one million double values).
It definitely warrants significant thought and design. It sounds like a Swift 4 thing, though I continually hope for Swift 3, as it is the main issue (i.e. How do I save information stored in value types?) stalling real-world projects which I have in the pipeline. If improved generics make their way into Swift 3, it should be enough, as we can make custom frameworks (which don’t crash the compiler) to do the job until the official version is ready...
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution