First impressions of Apple’s Swift programming language
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:
- Types – Swift uses var and let (constants) and types can be added optionally.
- 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.
- Operator overloading – As in the C++ language, you can define your own operators.
- 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.
- 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.
- 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.
- 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.
- 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.
- Closures – Closures are basically lambda expressions that allow to inline code fragments.
- Classes – Swift supports classes with single inheritance (and also structs without inheritance).
- 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.
- Optional Chaining – Protects from exceptions when calling multiple methods/properties in a chain where one call participant would return “nil”. Very handy.
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.