4 minutes
Swift as a viable Python alternative?
Recently Swift for Tensorflow has picked up some steam, so I wanted to explore the Swift programming language a bit.
The main advantage over Python for Swift is that Swift is very fast by directly using the LLVM compiler infrastructure. Python itself relies a lot on C to make code run fast, but if you write Python code you can get very slow code if it’s not optimized.
However, the main disadvantage for Swift is that it’s ecosystem when it comes to machine learning and data processing libraries is currently a lot less powerful than Python’s ecosystem. This could change in the future, of course.
So let’s check out some of Swifts features and be surprised that it looks in fact not very different from Python code.
Value assignments
Use let
to make a constant and var
to make a variable. The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once. This means you can use constants to name a value that you determine once but use in many places.
When declaring a constant in swift, you use let
. If you want the value to be variable, use var
instead:
“The width is 42”
In contrast to Python, Swift is a typed language. It allows to infer types dynamically, though, but once a type is established, you cannot assign a value of another type to it.
“error: cannot assign value of type ‘String’ to type ‘Int’”
Arrays and dictionaries
Using arrays is roughly like Python with the difference that a dictionary is also list-like:
bart simpson
homer simpson
marge simpson
Loops and conditionals
Except for using braces instead of indentation, it looks very Python like:
3
One thing I noted which is quite different from Python is the usage of so-called Optionals.
It’s a variable which indicates that it might have a value of a specific type or be None
(which is called nil
in Swift) and this is achieved by putting a question mark after the type.
You can then use the ??
operator to fall back to a default value if the Optional is nil
:
“Hi Guest”
“Hi User42”
Ranges
Instead of the Python range operator, Swift uses custom syntax.
When using ..<
, then the range is non-inclusive (like range in Python) and when using ...
the range includes the upper value.
1
2
1
2
3
Functions and Closures
Swift uses func
instead of def
to declare a function. The syntax is just as if you use types in Python with ->
to separate the parameter names and types from the function’s return type.
“Moin dear Reader.”
One odd thing to note: the parameters need to be passed in the exact order as they were defined. So this doesn’t work:
“error: argument ’name’ must precede argument ‘greeting’”
You can nest functions just like in Python and they also use closures as you are used to:
15
Functions can return other functions, the syntax might look a bit odd at first:
20
Lambda functions are written using {}
braces and can nicely be used in maps:
▿ 4 elements
- 0 : 5
- 1 : 10
- 2 : 15
- 3 : 20
But you can also write it less verbose:
[5, 10, 15, 20]
Instead of using a name, you can also refer to the parameters in a closure by number. When a closure is the only argument to a function, you need no regular brackets:
[4, 3, 2, 1]