Of all the announcements at WWDC the one that got me the most excited was that of Swift, the new programming language. I looked at Go and Rust earlier this year, and while I find them interesting I didn’t find a compelling reason to switch from Python, my trusty go-to language for the past few years. And even though I prefer Python for most projects, when you’re developing Cocoa applications you always feel like a second-class citizen. The crazy long method names are a small but ever-present annoyance, and I’ve spent way more time than I like trying to herd runaway exceptions in background threads.
So I’ve been on the fence lately, thinking that maybe I should trade some of Python’s ease of development for Objective-C’s promise of a more stable code base down the road. Thus, the Swift announcement couldn’t have come at a better time. The stability and performance of a modern, type safe language, together with the automatic memory management and flexible container types that Python has spoiled me with. What’s not to love?
I took the plunge and installed the Xcode 6 beta, and promptly lost a day futzing around with Storyboards and Auto Layout - I really need to take the time to learn that stuff. But then I spent most of yesterday making a silly little app to display the status of the Absolute Manage agent and what packages it has queued for install, giving Swift a proper workout. My impressions:
The Good
- The syntax is nice and consistent, with no obvious warts.
- All the things you’d expect from a modern language, like type inference and closures.
- Optionals provide an elegant and safe alternative to NULL pointers.
- It’s refreshing to actually have to read the documentation, since there’s virtually nothing of substance on Google or StackOverflow.
The Bad
- It’s frustrating to actually have to read the documentation, since there’s virtually nothing of substance on Google or StackOverflow.
- There’s no way to catch exceptions, yet we’re expected to call classes that can raise them.
- I’m missing list comprehensions.
The Ugly
- The Xcode syntax highlighter keeps crashing and restarting.
- I’ve managed to find no fewer than two bugs with array downcasting that causes the compiler to crash.
- Error messages are rarely helpful:
AMSController.swift:52:38: error: cannot convert the expression's type 'AnyObject[]!' to type 'AnyObject[]!'
if let urls = fm.contentsOfDirectoryAtURL(dirURL, includingPropertiesForKeys: [], options: 0, error: &error) {
~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(I think options: 0
should be options: nil
since in Swift it’s a struct and not a bitmask - at least that makes the compiler happy.)
Verdict
Overall the first impression is overwhelmingly positive, and whatever my next Cocoa project will be I’m certain that I’ll be writing it in Swift. The only problem is that the toolchain is currently too buggy for actual work, but I’m sure that will improve in the next couple of betas.