Overview of Swift programming language
Table of Contents:
Swift can easily become the most dominant language soon, beating the Python codding efficiency and the speed of C programming language.
In here, I wanted to provide an overview, focusing on what is possible in Swift 5+.
Swift history
Chris Lattner, creator of LLVM compiler, created Swift as an ambitious programming language.
From the very start Swift was designed to be expressive, flexible, and fast. It was design to express complex programming design patterns with ease and also deal with hardware and low level system stuff. Yes, you can write bootloaders in Swift.
In 2014 Apple released Swift version 1.2 to develop iOS and macOS applications. The plan was that Swift should replace Objective C one day.
Today Swift is definite replacement for Objective-C.
Swift is also open source, and you can contribute here.
Beside writing native iOS and macOS applications, you can create Linux applications in Swift, or install it using conda on almost any system and write efficient machine learning software.
To write efficient machine learning software recently, Swift added Automatic Differentiation Engine from Fortran.
To add more accent on machine learning, Swift creator Chris Lattner joined Google team. Together they are building S4TF (Swift for TensorFlow) – carefully planned and possible future dominant machine learning platform.
Swift promise
Swift promise is to be infinitely hackable platform. This means you can do anything in Swift, but to achieve this Swift often need to improve the syntax, and evolve.
Swift online info is often outdated
Since Swift is evolving so fast online Swift information is frequently out-of-date.
Why Swift is fast?
Reasons why Swift is fast:
- ARC
- no garbage collection
- smart usage of value types
- copy on write (COW)
- smart machine optimization module (LLVM)
- unsafe mode
Swift can often get the same performance(:rel=”nofollow” target=”_blank”) as carefully optimized C code.
ARC
Swift Programming Language states:
Reference counting is not a new concept, it is also used in Python. However, Python is interpreted language, where Swift is compile time language.
It is unique for a compile time language to have reference counting.
No garbage collection
Because Swift has ARC it doesn’t need garbage collection. Garbage collection would occur at a specific time. In fact you may call the reference counting as a specific type of garbage collection. However, Java or .NET garbage collection principles are fundamentally different.
.NET took Java garbage collection since early releases of .NET were nothing but Java cloning.
Value type semantics for all structures
Computer languages data types are either:
- value types or
- and reference types
Swift uses value types, and reference types according to Wikipedia
Value types are all structures:
booleans | characters | integers |
floating point | fixed point | strings |
tuples | arrays | dictionaries |
sets | stacks | queues |
enumerations | … |
Reference types are:
- classes
- functions
- interfaces
Let’s compare Python and Swift working with integer.
Swift example:
// working with Int
var a=1
var b=a
a+=1
print(a)
print(b)
// working with Array
var x: [Int] = [1]
var y=x
x.append(2)
print(x)
print(y)
Output:
2
1
[1, 2]
[1]
In case of Swift everything looks good. Working with Int or Array
Let’s check the same results for Python.
Python example:
# Working with Integer
a = 1
b = a
a+=1
print(a)
print(b)
# Working with array
x = [1]
y = x
x.append(2)
print(x)
print(y)
Output:
2
1
[1, 2]
[1, 2]
You may be familiar with Python reference semantics and understand the if you alter x, y will also be altered.
So to be correct, Python solution would be to use copy
function like this:
# Working with array
x = [1]
y = x
y=y.copy()
x.append(2)
print(x)
print(y)
Output:
[1, 2]
[1]
This will provide the correct output, just as Swift.
You may be familiar with C#. In C#
struct
will give value type,andclass
will give reference type.
In Swift all
struct
types are value types.
Copy On Write (COW)
Often referred as COW. Here is how COW works.
Example COW on array:
import Foundation
func addr(of object: UnsafeRawPointer) -> String {
let addr = Int(bitPattern: object)
return String(format: "%p", addr)
}
var a1 = [1, 2, 3]
var a2 = a1
var a3 = a1
print(a1, addr(of: a1))
print(a2, addr(of: a2))
print(a3, addr(of: a3))
a3.append(5)
print(a1, addr(of: a1))
print(a2, addr(of: a2))
print(a3, addr(of: a3))
Output
[1, 2, 3] 0x7ff13ef0d898
[1, 2, 3] 0x7ff13ef0d898
[1, 2, 3] 0x7ff13ef0d898
[1, 2, 3] 0x7ff13ef0d898
[1, 2, 3] 0x7ff13ef0d898
[1, 2, 3, 5] 0x106e200
In Swift arrays a1, a2, a3 will have the same memory address at first. After using the mutating method append
array a3 will be cloned and new memory address will be assigned for a3.
Unsafe mode
There is a special Swift unsafe mode where Swift doesn’t check bounds of arrays, dicts, or ranges. This mode is considerable faster than the safe mode.
Swift is hackable
This means Swift took gems from other programming langues. Also it meaning you can add your own methods and operators on any type – your custom types or types from the standard library, or any library. This also means Swift can call into Python and C.
Calling into Python from Swift
Example:
%include "EnableIPythonDisplay.swift"
IPythonDisplay.shell.enable_matplotlib("inline")
import Python
public let np = Python.import("numpy")
public let plt = Python.import("matplotlib.pyplot")
let nparray = np.array([1,2,5,1])
print(nparray)
plt.plot(nparray)
plt.show()
plt.close()
Output:
Yes, this is possible. Just import Python in Swift (import Python
), and from there import all the libraries you use to work with.
Calling into C from Swift
Similar approach would be to call into C from Swift. When Python code needs to call into C, it performs slow, because Python has GIL (Global Interpreter Lock) problem. Swift, on the other hand, is made to work with C-like languages by design.
Swift can deal with C header files thanks to the Clang engine part of LLVM. Clang can deal with C/C++/Objective C and even CUDA. Long term, Swift is trying to subtract C/C++ code out of the picture, because Swift is the new C.
Where can I try Swift fast?
You can start using Swift from Google Colab. The other resource is the Online Swift Playground.
Tour of Swift
Here is the detailed guided tour to Swift.
…
tags: intro & category: swift