Swift is an awesome new language

I don't have a quirky-but-funny name for this post - I consider it too important to be clever about it.

I was there in the room when Apple announced Swift but to be honest, apart from a big applause, I don't remember much as I was too tired from waiting since 3 AM to get into the keynote (note: really not necessary - a friend of mine arrived at 9:15 and got in). The first impression was good but I thought that they might have adopted something already existing and known to others.

Over the week I have attended or saw all Swift session on WWDC 2014 and my opinion changed: Swift is great and I now consider that Apple did exactly what they should have done. Not only for them but also for their customers and for us the developers. There are several reasons for this change of heart.

Swift is deterministic

As John Siracusa argued in The Dark Age of Objective-C, Objective-C got a new lease of life back in 2008 with the release of iOS SDK. In it Apple had (and still has) close-to-the-metal language which, for mobile devices, even to this day, is critically important for performance and battery life (race to sleep and all that). Yes, the language itself is not very good but how its output was and is great for the problems it has to solve. It is a compiled language with deterministic behavior and, since then, greatly improved reference counted memory management.

On the other hand, other major mobile device frameworks were built around garbage-collected languages and virtual machines:

(I'm not mentioning others as I don't know enough about them)

I have a lot of experience with C#, though not on mobile devices, and while I consider it a great language and .NET a great platform, I always missed the deterministic nature of C and C++. This deterministic nature was especially important for predictable resource disposal of which memory is just one. Instead of built-in-for-all determinism these languages offered facilities like using and lock in C# thus forcing end-developers to do more work than library providers.

Some may say that garbage collection is the how the memory management should be done but I weakly disagree - it's not that it's how it shouldn't be done, it's the case of the right tool for the right job and there is only negligible price to pay when using reference counting like ARC does for both Objective-C and Swift. I know this because in the past 10 years I have spent most of my working time designing, building and maintaining a medium-sized (tens of thousands of lines) but complex C++ library with integration to C++/CLI. In all that time, thanks to smart pointers and especially their implementation in Boost (more on Boost below), I literally wrote only one (one!) manual delete for objects and even that only because Windows thread pool is a C API and uses void*. This library has hundreds of types and 99.999% of the memory it consumed are small(ish) C++ objects generated on the heap. And there was never a need to manually dispose of objects and in fact, as the lead, I prohibited working with naked pointers.

Swift is fast

This ties back into the close-to-the-metal philosophy that Apple is applying and why in part is has an edge over its competitors in mobile space. This is how, among other things like having its own CPU, it maintains the right (for its users) trade-offs of battery duration, performance and weight/volume of its devices.

Swift was designed in such a way as to allow even better optimizations than it was previously possible, in a sort of reverse of Intel's failed Itanium efforts. Apple wrote a language for which it can do far more optimizations than for other languages that it doesn't control.

Swift also adopted, where it could, one of the core values of C and C++ and that is to pay in performance only for those language features that are actually used. So for example structs have zero runtime cost as they aren't created on heap nor they are virtualized.

Swift is safe

And by "safe" I mean it was designed and implemented to be easy to program safely in it and hard to make a mistake. To give you an example, I wish it wasn't keeping the curly braces but adopting indented semantics like Python or CoffeeScript. But how its keeping the curly braces is critically important - in Swift they are obligatory. Again, the mind boggles thinking about how much time we have collectively wasted debugging issues due to optional curly braces in the likes of C, C++, Objective-C, Java, JavaScript, C# and so on (not to mention curly braces and macro expansions...) Most coding standards (and certainly the few that I wrote) force programmers to always use the curly braces but this is obviously a fragile solution, even with automated checks in place. I'm glad to see it go away in Swift.

Another example is the way optionals are used and built where not all but only optional references can be nil. The NULL-ness or lack of it is a common source of errors and Swift makes the choices here deliberate.

What to say of let? I was const freak in C++ until I saw that it was giving us negative returns by increasing the code complexity (in some cases quadruplicating the class function count) with little, if any, real benefit. Data immutability is critically important for more efficient concurrency but in C++ it wasn't designed for that but as a way to make better APIs. In Swift making variables immutable is so easy and it's controlled by the compiler so it cannot be just cast away like in C++ (and I'm sure they aggressively optimize on such values). I just wish it was "deep" for references as it is for values.

Swift is backward compatible

Swift code, by sharing the same runtime as Objective-C, can be compiled for pre-8 iOS and pre-10.10 Mac OS X. This couldn't have been easily achieved if Apple decided to completely change the stack by changing the runtime. So this is another nice bonus for developers and users.

Swift also has transparent interaction with C, Objective-C and C++ allowing us to leverage already existing code (including Boost!)

Swift is proprietary...

...and that's a feature, not a bug. The source of my biggest frustrations and disappointments in the years (decades!) of working with C++ was its glacial pace of innovation on the language level. Back in 2002 I wrote to Andew Koenig regarding type inference and asking if there was a chance of its inclusion in then-upcoming C++ Standard. He was skeptical but at the end the type inference was included... in C++ Standard from 2011, 9 years later! The amount of time the programmers have collectively wasted in those 9 years... I have no way of calculating but it's very likely mind-boggling. What to say of things like stable ABI and modules - those might not come before I retire of old age. So in time I grew disillusioned with and tired of C++. All the people on the standardization commitee were trying, struggling, fighting but it was just too slow for me. I still loved the language but the way it has (not) evolved didn't love me back at all. It's built by and for institutions and it will far outlive all of us but it wasn't for me anymore.

In those same 9 years C# went from a Java look-alike to an amazing language chock-full of great stuff. And this was possible because it was not designed by a committee but by an empowered group of very talented people at Microsoft. Yes, C# is also standardized but only half-heartedly so - the real development of it was always done by Microsoft.

Apple is far bigger control freak than Microsoft and it will control Swift for its own purposes and again that's a good thing. It's likely that there will be a bunch of stuff that I will deeply dislike but the alternative of stagnation is much worse. And in time we will very likely see bindings for other non-Apple operating systems and frameworks (not done by Apple of course but by others).

Swift is innovative

There are things in Swift (of which I'm not allowed to talk or write as all the WWDC content is under NDA... which makes it easier for me to pretend I know something others don't ;) that have clearly been taken from many other languages. But there are other things like generic enums and pattern matching which I haven't seen before and which I found very useful in the way they are intended to be used.

Anyway, as I was getting to know the language, I found myself continuously nodding in approval, seeing what they brought together from different sides. Chriss Lattner, the language's original creator, says that much himself:

[Swift] also greatly benefited from the experiences hard-won by many other languages in the field, drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list.

They brought a lot of great stuff from other languages, but there is still this overarching theme of less typing, safe programming and more expressivity. And a lot of the stuff I saw was like from a wish list of a disillusioned C++ programmer.

It all made more sense (and affirmed my growing appreciation of the language) when I saw Dave Abrahams take the stage today on a Swift session. This is a man that among other things:

  • was a member of standardization committee of C++ 1998 for which it formalized exception guarantees (which you should understand in whatever language you may happen to work)
  • co-founded Boost.org, a set of open-source libraries with common license, review process and drive for engineering excellence which greatly helped worldwide C++ community
  • wrote or co-wrote some great libraries in Boost
  • co-wrote a book on template metaprogramming in C++

Seeing Dave Abrahams as not only advocate but one of the creators of Swift gave me assurance that this language is something to look forward to (not to detrement of other creators - it's just that I'm very familiar with Dave's previous work). After the session I approached him seeking to thank him for Boost and all the things he has done for the state of art of programming. He was really nice about it and we talked a bit about what he thinks of the project but I'm not sure if such conversations are also covered by NDA and far more importantly I didn't ask him if he would mind if I quoted him so I won't. Sufficient to say that for me it was the final confirmation of Swift and what it will likely come to mean for programming community.

Summary

To sum it all up, just like they do in WWDC sessions, Swift is:

  • Deterministic in object lifetime and resource acquisition and releasing
  • Compiled to machine code, greatly optimized and very fast
  • Safe to program in
  • Backward compatible
  • Under control of a single company (read: group of people) which limits the scope of its usefulness but greatly improves the depth of it and accelerates its evolution
  • Innovative and evolves the state of the art and takes the best parts of many other languages

I have started learning and playing with it and I look forward very much to the projects I intend to create with it.