First Impressions of Swift

Article summary

My programming background is primarily one of Ruby, Javascript, C#, and C. Nevertheless, I was intrigued when Apple announced Swift at WWDC this year.

I tend to pick up new programming languages like a crow picks up shiny objects, so when I found out that Swift was to support type inference, Option types, Sum types, a real REPL, and nice lambda syntax, I had to check it out further.

Good Bits

Type Inference
Like many other recent languages, Swift supports local type inference. Instead of having to explicitly declare the type of local variables and constants, Swift’s compiler will figure it out for you!
Tuples (see also Product Types) are a nice way of handling small bundles of related state. They allow for efficient and easy multiple return values, among other things.
Pattern Matching
This can be used to great effect in their case statement, giving an extremely powerful form of flow control.
Optional Types
Similar to Scala’s Option type, Swift’s Optional Types offer a cleaner handling of information that isn’t necessarily present. You’ll also find a somewhat similar concept embodied in C#’s Nullable types.
The Playground
Swift’s Playgrounds initially reminded me of the Worksheet feature in Scala’s Eclipse IDE. After some fiddling and watching the intro video from WWDC, they appear to be considerably more powerful. It’s nice to see an interactive development environment from a company as large as Apple.
Swift has a REPL that works quite well. It’s great for fiddling around with code on the command line, and it makes quick experiments in syntax and features really easy.
Objective C Interoperation
One of Swift’s most exciting features is it’s Objective C interop story. Because it’s so easy to move between the two languages, you can leverage all of the existing Apple APIs and any libraries or code you’ve already written.

Bad Bits

Lacking Destructuring Assignment
Swift is missing some of the destructuring I’ve come to know and love in languages like Ruby and Clojure. You can do a destructuring bind with tuples, but there’s no support for destructuring higher-level collections, like arrays or dictionaries.
Flow Control Statements
In languages like Haskell, Scala, and even Ruby, if/else/case statements are expressions, not statements. This means that they return a value. This makes some code much more concise. In Swift, however, if/else/case are all statements.
Swift uses Apple’s Automatic Reference Counting to manage your application’s memory. I’ve worked with manually- and automatically-managed languages before, but ARC seems to occupy an odd middle ground. You don’t need to worry about memory management most of the time, but when you do, it becomes extremely important.
Closed Source
Swift is based on LLVM, but as of yet, has not seen any sort of source release. There’s always the hope that Apple will open up the compiler later, but it would be nice to have it opened now. Opening the source would allow further development of new language features and a tighter community feedback loop.

Discussion Elsewhere

Swift has generated quite a bit of buzz. Here are some of the better discussions of it that I’ve seen pop up so far.

In Conclusion

Swift is an extremely interesting looking language, and one which I will hopefully be building things in the near future. While I’ve been an Apple user for some time, none of the system-native programming had caught my attention. Swift appears to be a flexible, powerful modern language, with a backing of interesting targets and well-designed APIs. It appeals to my inner programming language geek with powerful features like Type Classes (Scala version), while still being easily readable to someone familiar with a language like C#.

Do you have any favorite features in Swift that I’ve missed? Give me a shout in the comments.

  • Jean-François Héon says:

    Apart from being closed-source, ironically making C# a more interesting option for me for cross platform development, my biggest disappointment are flow control statements instead of expressions.

    Oh well, no language is perfect.

  • Comments are closed.