First look: Apple's Swift is simple, at first
Swift's clean and modern syntax makes it quick and easy to get started, but mastering this new language will take a while
That high-level road map and a few details about the syntax of declaring variables is all you need to get going. Within a few days of Apple's announcement, open source software written in Swift started appearing on GitHub. One of the most impressive, a clone of Flappy Birds by Nate Murray, required just two major Swift files and a bit of glue code. If you know your way around OS X or iOS programming, you can get moving very quickly. Murray says he spent only a day on his clone.
Swift fixes a few of the biggest complaints about Java syntax. You won't need to waste keystrokes spelling out the type of each variable; the compiler will infer it from initialization. You get all of the advantages of letting the compiler check the types without many of the keystrokes. If you want to glue together variables and strings, a simple templating hack uses a mere three keystrokes to insert a variable into a string. It's a masterstroke.
There are other neat additions. Xcode now offers a "playground" where you can type code and watch it execute as your fingers move. Sandboxes like these have made learning JavaScript and many JavaScript APIs much easier. Google, for instance, has lured many developers to its Maps API merely by offering a Web page with a playground in it. Xcode's playground is even better than most because it displays a running interpretation of the code in a gray section to the right, letting you see how the language operates. The only negative I'll register about the playground is that mine crashed repeatedly in ways that couldn't be reproduced. It's not like I tried to divide by zero. It bonked, and the next time it worked.
For all of the advances and improvements, I found the size of Swift to be a bit daunting. While it's easy to see how Nate Murray knocked off Flappy Birds in a day, many other details of the language can't be mastered in a few minutes. I spent some time puzzling about the difference between data that's passed around by value (struct
) and data that's passed around by pointers (class
). Juggling these differences is one of the big challenges to C programming, and it will remain an issue for Swift programmers who want to squeeze the best performance out of their code.
Optimization will probably be a real headache for some kinds of code. Strings, for instance, are always passed by value, which means Swift will be making copies of strings left and right with every function call. This may not be much of an issue for graphics-based programs like games, but programs like Web servers and content management systems are essentially concatenation machines. Anyone who starts juggling strings will need to pay attention to this when writing Swift code. They may end up wrapping the string values in classes to avoid the copying.
There are a number of other areas that I earmarked for deeper study. Downcasting, for instance, lets you shift the focus from a class to a subclass with the typecasting operator (as
). This may fail, so there's another typecasting operator with a question mark (as?
), which returns the value of nil
if the downcast doesn't succeed. There are a number of these optional features where the presence or absence of the question mark changes the behavior of keywords. Another feature, subscripts, lets you dig into a class quickly to pull out a particular value. It's neat shorthand, but it has the potential to confuse.