First impressions of Apple’s Swift programming language

Apple Swift Logo 150x150 First impressions of Apples Swift programming language

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, <more elements>) 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 icon wink First impressions of Apples Swift programming language ) 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.

16 Responses to “First impressions of Apple’s Swift programming language”

  1. Mark says:

    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”.

  2. John says:

    @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’.

  3. Moritz Post says:

    @Mark Yes agreed. “let” is a not so obvious choice. “const” is very known. My guess is that they have used “let” to make it clear that it also played together with “optional bindings”.

  4. Steven S. says:

    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, …

  5. Moritz Post says:

    @Steven That is a very good point. I didn’t find anything about concurrency in the docs either. Maybe you have to fall back to Objective-C in that case for now. Could be a weak point indeed.

  6. chris brind says:

    RE: Concurrency … use Grand Central Dispatch as before. :)

  7. chris brind says:

    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… ;)

  8. Randal L. Schwartz says:

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

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

  9. PeiSong says:

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

  10. Moritz Post says:

    @Randal Ups you are right. Sorry about that. corrected. :)

  11. Alexander says:

    Actually, extensions existed in Objective C as well

  12. Greg Lutz says:

    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.

  13. Mark says:

    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 http://www.infoq.com/presentations/top-10-performance-myths
    (i could be wrong and it isnt the same thing)

  14. Moritz Post says:

    Apple claims huge performance improvements and while that might be true for several tasks it seems to be false for other mundane operations.

    This blog posts suggests that due to using objects rather than c scalars swift is actually slower in many places. http://www.splasmata.com/?p=2798

  15. Mark says:

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

    http://www.codenameone.com/blog/codename-one-benchmarked-with-amazing-results

    Also objectivec ain’t c.

  16. iGerbil says:

    Just found a nice Swift training, it gives you a feeling of what it’s all about….
    http://www.udemy.com/swiftdeveloper

16 responses so far

Written by . Published in Categories: EclipseSource News, Editors choice