First look: Apple's Swift is simple, at first
- 25 June, 2014 20:07
What is it about being a rich corporation? After the private jets and the gleaming headquarters comes the new programming language. Now Apple has followed in the path of Microsoft (C#), Sun (Java), and Google (Dart, Go) to offer us Swift, a language with a C-like syntax and inferred typing designed to make it easier for the average coder to start building new software for the Mac and new apps for the iPhone.
While it's never possible to overestimate the amount of hubris at Apple, Swift is more than a vanity project to dictate how the world speaks a computer version of the King's English, if you will. Apple's software stack has been stuck with Objective-C since NeXT invaded the company, and many neophytes find it a pain to untangle the pointers, the files (both .m and .h), and the punctuation-rich syntax.
[ Also on InfoWorld: 10 features Apple "stole" for the Swift programming language. | See InfoWorld's "iOS 7 for developers" special report for the scoop on the bells and whistles in Apple's mobile OS -- and how you can harness them. | Keep up with key Apple technologies with the Technology: Apple newsletter. ]
Apple tried to update the language with a "modern syntax" in 1997, but the masses refused to budge from the so-called classic syntax. Other projects -- like adding Java bindings for the Cocoa libraries -- never really stuck. Today, learning Objective-C is one of the biggest hurdles to getting people to code for the Apple platforms.
The basic structure for a Swift program looks much like Java. The code is broken into classes filled with fields and methods. The most jarring difference is that the methods start with a keyword (func) and the return value comes after the method name, not before it. The classes can be aligned in a hierarchy with plenty of inheritance for object-oriented code, and the properties can be read with a dot syntax.
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.
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.
The Playground evaluates your Swift code as you type it. You type code on the left while it describes what's happening internally on the right.
A major commitmentThe simplicity of the Swift language is deceptive. While it's easy to write a "hello world" app in one line and build a quick app with a few lines of code, numerous details and nuances will take plenty of study to master. The adept programmers will love the power and the ability to use the clever tricks in their software, but many neophytes will find it hard to read their code. This will not be an easy language to master, no matter how easy it might be to pick up.
The most important thing to recognize about Swift is the scope. While some of new languages have a toylike feel because they're relatively small projects, Swift is a major commitment. The first book on Swift alone is excellent, in part because it's more than 850 pages long. The Xcode download now pushes 2.5GB. The language isn't a little playpen for easy coding, but a full software package designed to offer all of the support a team of programmers needs to take on major projects.
The language also includes copious hooks and tools for working with the existing Cocoa libraries. Apple doesn't want to rewrite the iOS and OS X stacks, so Swift makes linking to them simple. This entire project was built to maintain the interest in these stacks and to make it easier for anyone to walk in and start coding up apps. At the same time, you can use as much or as little Swift code in your iOS or OS X project as you like.
Apple isn't forcing a march to Swift. No doubt many of the old guard will stick with Objective-C. But Swift will make it much easier for programmers steeped in other languages and traditions to pick up Xcode and quickly become productive. That alone is a big step forward for Apple and one that will probably lead to even more native iOS and OS X code than ever. Swift doesn't need to take over the world to be a gift to it.
This article, "First look: Apple's Swift is simple, at first," was originally published at InfoWorld.com. Follow the latest developments in application development and mobile technology at InfoWorld.com. For the latest business technology news, follow InfoWorld.com on Twitter.
Read more about application development in InfoWorld's Application Development Channel.