First impressions of Apple's Swift programming language

June 3, 2014 | 3 min Read

Apple Swift logo is a trademark of Apple Inc.

Apple introduced the Swift programming language at its wwdc14 event yesterday. I spent some time digging through the language documentation. Here are my initial thoughts on some of the more interesting features.

The language is much more concise. If you have a background in any C-based language (other than Objective-C), you will be able to read it. Swift has many language constructs popularized in other languages. It is pretty close to what is hot in programming languages today.

Here are some features of note:

  1. Types - Swift uses var and let (constants) and types can be added optionally.
  2. Optionals and Optional Bindings - An optional type is a type that might contain a value of a type. It allows you to more easily convert from type A to B and circumvent the dreaded null check.
  3. Operator overloading - As in the C++ language, you can define your own operators.
  4. Range - A Range is an expression in the form 5…8 and can be used in control statements like if, switch, loops etc. and also in array operations. Eg. replace element 5 to 8 with elem1. The feature seems to be inspired by Python.
  5. Tuples - A tuple in the form of (code, message, ) can be used as a sort of a lightweight object. Quite handy I guess and another feature from Python.
  6. Collections - While I think collections are one of the most important aspects of a language, Swift only has Array and Dictionary (which are typed). The usual operations can be performed on them but it does not seem to be the strong part of the language.
  7. Switch statement - The switch statement is quite powerful. It has no default fallthrough and is able to switch on a multitude of conditions (including ranges, list of elements, boolean expression, enums, …). Seems to be useful.
  8. Functions - Functions offer basically every feature you could imagine: optional types, default types, named-arguments, optional arguments, varargs (Variadic Parameters <- nice name ;) ) and awkward parameters that can be changed from inside the function. Functions can also be used as types and can be passed around as variables.
  9. Closures - Closures are basically lambda expressions that allow to inline code fragments.
  10. Classes - Swift supports classes with single inheritance (and also structs without inheritance).
  11. Properties - Classes can have methods but also Properties. Properties are a little reminiscent of C# or dart properties and have (optional) get and set methods.
  12. Optional Chaining - Protects from exceptions when calling multiple methods/properties in a chain where one call participant would return “nil”. Very handy.
  13. Extensions - An extension is probably pretty close to a prototype in Javascript or a C# extension as it allows to retrofit an existing class with new methods or properties.

So there you have it: some of the noteworthy features of Swift. It should all be taken with a grain of salt as I have very limited experience with the language. Nonetheless, I really like what I see.