First impressions of Apple’s Swift programming language

First impressions of Apple’s Swift programming language

Apple Swift logo

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.

  • Mark
    Posted at 10:28 pm, June 3, 2014

    Compared to Objective-C, one has no choice but to like it. ๐Ÿ™‚ LOL. I like what i see too. If someone knows Groovy or any of the newer JVM languages (to include Java with XTend) they should be able to get Swift.

    I am not sure however why “let”? I mean, you get rid of semi-colons and other things. But i gotta type “let”.

  • John
    Posted at 8:18 am, June 4, 2014

    @Mark, ‘let’ is shorter than ‘const’, but it reminds me of BASIC too much. ‘const’ would have been better as the semantics is implied, just like ‘var’.

  • Steven S.
    Posted at 11:54 am, June 4, 2014

    I took a quick look at the features and there seems to be nothing about concurrency/parallelism…
    In this multi-core age this is a big defect for me. So this “fantastic” language that Apple invented seems like a dumbed down version of better established multiplatform languages like C#, Scala, Groovy, C++2011 & Qt5, …

  • chris brind
    Posted at 5:52 pm, June 4, 2014

    RE: Concurrency … use Grand Central Dispatch as before. ๐Ÿ™‚

  • chris brind
    Posted at 6:03 pm, June 4, 2014

    RE: const – this is just my opinion but the range of things you can do with const in C, specifically around pointers actually means that const is pretty vague. So in this new language I think they decided *let*’s be explicit… ๐Ÿ˜‰

  • Randal L. Schwartz
    Posted at 10:51 pm, June 4, 2014

    “Operator overloading โ€“ As in its originating C language, you can define your own operators.”

    Uh, no. You must be thinking of C++.

  • PeiSong
    Posted at 4:38 am, June 5, 2014

    My guess is the concurrency is handled on cocoa framework level.

  • Alexander
    Posted at 1:59 pm, June 9, 2014

    Actually, extensions existed in Objective C as well

  • Greg Lutz
    Posted at 1:53 am, June 10, 2014

    Swift *does* allow you to define operators, and even to define your own operator *tokens* (like *+-/ if you wanted to). What it currently lacks is exceptions and class member access control (everything’s public). It’s hard to believe that it will go into serious production lacking those features.

  • Mark
    Posted at 5:04 am, June 10, 2014

    So “let” actually means something. Of all the languages I know, I’ve not seen anything like it. Must google…

    As for concurrency and parallelism, i suggest you watch
    (i could be wrong and it isnt the same thing)

  • Mark
    Posted at 3:38 am, June 12, 2014

    According to this objectivec isn’t that tough to beat.

    Also objectivec ain’t c.

  • iGerbil
    Posted at 4:05 am, June 23, 2014

    Just found a nice Swift training, it gives you a feeling of what it’s all about….