Swift is a powerful new language, with a rapidly growing user base—which now includes Linux server-side developers. This presentation is a briief overview of the current Swift web server frameworks, as well as a highlight of the best features that Swift has to offer.
3. • A new programming language (2014)
• Developed by Chris Lattner & Apple
• Compiled, based on LLVM stack
• Static, strong, and inferred typing
• Multi-paradigm (OO, functional, imperative, protocol-oriented)
• Focused on speed & safety
Swift
What even is it?
4. • A Joy to Work In
• Expressive, yet concise
• Safety Front-and-Center
• Incredibly Intelligent Compiler
• Robust Type System
• Rapidly growing in popularity
Why Swift?
Here’s why we love it
5. Swift Evolution
• Hello World!
• Core language features
• Apple platform support
Initial Release
1.0 (2014)
6. Swift Evolution
• Hello World!
• Core language features
• Apple platform support
Initial Release Open Source
1.0 (2014) 2.0 (2015)
• OSS compiler & standard
library
• Linux support
• Error handling
• Protocol extensions
7. Swift Evolution
• Hello World!
• Core language features
• Apple platform support
Initial Release Open Source Community-Driven
Refinement
1.0 (2014) 2.0 (2015) 3.0 (2016)
• OSS compiler & standard
library
• Linux support
• Error handling
• Protocol extensions
• All changes via community
evolution process
• Major syntax changes for
coherence & consistency
• Std Lib rewrite
• Official API design
guidelines
8. Swift Evolution
• Hello World!
• Core language features
• Apple platform support
Initial Release Open Source Community-Driven
Refinement Stability & Polish
1.0 (2014) 2.0 (2015) 3.0 (2016) 4.0 (2017)
• OSS compiler & standard
library
• Linux support
• Error handling
• Protocol extensions
• All changes via community
evolution process
• Major syntax changes for
coherence & consistency
• Std Lib rewrite
• Official API design
guidelines
• Strings enhancements
• Automatic archival &
serialization
• Reached top 10 in both
TIOBE & IEEE language
indexes
• Server APIs Working
Group
10. Swift Server-Side Frameworks
The Major Players
10k ⭐️
Community-Driven
Version 2.1
Vapor
12k ⭐️
Driven by community &
PerfectlySoft
Version 2.0
Perfect
6k ⭐️
Driven by IBM & community
Version 1.7
Kitura
13. 0
0.7
1.4
2.1
2.8
Perfect Kitura Vapor Node
2
2
1
3
Average Latency (ms)
Server Performance Shootout
• HTML Requests/Second
• HTML Average Latency
https://github.com/rymcol/Linux-Server-Side-Swift-Benchmarking
14. 0
5000
10000
15000
20000
Perfect Kitura Vapor Node
20K
11K
14K
9K
Requests/Second
Server Performance Shootout
• HTML Requests/Second
• HTML Average Latency
• JSON API Requests/Second
https://github.com/rymcol/Linux-Server-Side-Swift-Benchmarking
15. 0
0.6
1.2
1.8
2.4
Perfect Kitura Vapor Node
1.0
1.9
0.8
2.2
Average Latency (ms)
Server Performance Shootout
• HTML Requests/Second
• HTML Average Latency
• JSON API Requests/Second
• JSON API Average Latency
https://github.com/rymcol/Linux-Server-Side-Swift-Benchmarking
16. 0
4
8
12
16
Perfect Kitura Vapor Node
1.30 1.40 1.40
15.30
Memory Footprint (MB)
Server Performance Shootout
• HTML Requests/Second
• HTML Average Latency
• JSON API Requests/Second
• JSON API Average Latency
• Memory Footprint (Startup)
https://github.com/rymcol/Linux-Server-Side-Swift-Benchmarking
17. 0
75
150
225
300
Perfect Kitura Vapor Node
3
174
246
61
Memory Footprint (MB)
Server Performance Shootout
• HTML Requests/Second
• HTML Average Latency
• JSON API Requests/Second
• JSON API Average Latency
• Memory Footprint (Startup)
• Memory Footprint (High Load)
https://github.com/rymcol/Linux-Server-Side-Swift-Benchmarking
22. func logMessage(message:String,
prefix: String,
suffix: String) {
print("(prefix)-(message)-(suffix)")
}
logMessage(message: “Beautiful”,
prefix: "Hello",
suffix: “World”)
Named Arguments
SWIFT’S GREATEST HITS
Clarity at the point of use
23. func logMessage(_ message:String,
withPrefix prefix: String,
andSuffix suffix: String) {
print("(prefix)-(message)-(suffix)")
}
logMessage("Beautiful",
withPrefix: "Hello",
andSuffix: “World")
Named Arguments
SWIFT’S GREATEST HITS
Clarity at the point of use
25. let meaningOfLife: Int = 42
let pi: Double = 3.14159
let anotherPi: Double = 3 + 0.14159
let apple: Apple = Apple()
let appleDescription: String = apple.toString()
Type Inference
Only what’s needed
SWIFT’S GREATEST HITS
26. let meaningOfLife = 42
let pi = 3.14159
let anotherPi = 3 + 0.14159
let apple = Apple()
let appleDescription = apple.toString()
Type Inference
Only what’s needed
SWIFT’S GREATEST HITS
27. let currentPoints: UInt = 42
let miniPi: Float = 3.14159
var color: UIColor
if pi > 4 {
color = .green
} else {
color = .red
}
Type Inference
Only what’s needed
SWIFT’S GREATEST HITS
39. var myObject: Object = nil // <—- 🚨👾🚨*
*👾 == “compiler”
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
40. var myObject: Object = Object()
var optionalObject: Object? = nil
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
41. var myObject = Object()
var optionalObject: Object?
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
42. var myObject = Object()
var optionalObject: Object?
myObject = nil
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
43. var myObject = Object()
var optionalObject: Object?
myObject = nil // <—- 🚨👾🚨
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
44. var myObject = Object()
var optionalObject: Object?
myObject = otherObject // <—- ✅👾✅
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
45. var myObject = Object()
var optionalObject: Object?
myObject = otherObject
optionalObject = Object()
optionalObject = nil
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
46. var myObject = Object()
var optionalObject: Object?
myObject = otherObject
optionalObject = Object()
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
47. var myObject = Object()
var optionalObject: Object?
myObject = otherObject
optionalObject = Object()
myObject.print()
optionalObject.print()
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
48. var myObject = Object()
var optionalObject: Object?
myObject = otherObject
optionalObject = Object()
myObject.print() // <—- guaranteed to be ✅
optionalObject.print()
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
49. var myObject = Object()
var optionalObject: Object?
myObject = otherObject
optionalObject = Object()
myObject.print() // <—- guaranteed to be ✅
optionalObject.print() // <—- 🚨👾🚨
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
50. var myObject = Object()
var optionalObject: Object?
myObject = otherObject
optionalObject = Object()
myObject.print() // <—- guaranteed to be ✅
optionalObject?.print()
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
51. var myObject = Object()
var optionalObject: Object?
myObject = otherObject
optionalObject = Object()
myObject.print() // <—- guaranteed to be ✅
optionalObject?.print() // <—- has value, so ✅
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
52. var myObject = Object()
var optionalObject: Object?
myObject = otherObject
optionalObject = Object()
myObject.print() // <—- guaranteed to be ✅
optionalObject?.print() // <—- has value, so ✅
optionalObject = nil
optionalObject?.print() // <—- no value == no op
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
53. func setAlarm(time: Date, message: String?) {
alarmTime = time
if let message = message {
message.uppercased()
addExclamations(message)
emojify(message)
alarmMessage = message
}
}
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
54. func setAlarm(time: Date, message: String?) {
alarmTime = time
guard let message = message else {
return
}
message.uppercased()
addExclamations(message)
emojify(message)
alarmMessage = message
}
Optionality
Make nil Great Again
SWIFT’S GREATEST HITS
56. Enums & Generics
enum CompassPoint {
case north
case south
case east
case west
}
var directionToHead = CompassPoint.west
var directionToShore: CompassPoint = .east
Safe, yet flexible
SWIFT’S GREATEST HITS
58. Enums & Generics
switch directionToTravel {
case .north:
print("To Canada!")
case .south:
print("To Mexico!")
} // <—- 🚨👾🚨 Must be exhaustive!!
Safe, yet flexible
SWIFT’S GREATEST HITS
59. Enums & Generics
switch directionToTravel {
case .north:
print("To Canada!")
case .south:
print("To Mexico!")
default:
print("Into the ocean!")
}
Safe, yet flexible
SWIFT’S GREATEST HITS
60. Enums & Generics
enum Planet: Int {
case mercury = 1
case venus = 2
case earth = 3
case mars = 4
case jupiter = 5
case saturn = 6
case uranus = 7
case neptune = 8
}
Safe, yet flexible
SWIFT’S GREATEST HITS
62. Enums & Generics
enum Planet: Int {
case mercury = 1, venus, earth, mars, jupiter,
saturn, uranus, neptune
}
if let somePlanet = Planet(rawValue: 11) {
switch somePlanet {
case .earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
} else {
print("There isn't a planet at position (positionToFind)")
} // Prints "There isn't a planet at position 11"
Safe, yet flexible
SWIFT’S GREATEST HITS
63. Enums & Generics
enum Either {
case str(String)
case num(Double)
}
var a = Either.str("hello")
a = .num(1.0)
Safe, yet flexible
SWIFT’S GREATEST HITS
66. Enums & Generics
enum Trade {
case Buy(stock: String, amount: Int)
case Sell(stock: String, amount: Int)
}
func executeTrade(_ trade: Trade) { /* magic */ }
let purchase = Trade.Buy(stock: "AAPL", amount: 20)
executeTrade(purchase)
let sale = Trade.Sell(stock: "SNAP", amount: 500)
executeTrade(sale)
Safe, yet flexible
SWIFT’S GREATEST HITS
67. Enums & Generics
enum Either {
case str(String)
case num(Double)
}
var a = Either.str("hello")
a = .num(1.0)
Safe, yet flexible
SWIFT’S GREATEST HITS
68. Enums & Generics
enum Optional<Wrapped> {
case some(Wrapped)
case none
}
var possibleInteger: Optional<Int> = .none
possibleInteger = .some(100)
Safe, yet flexible
SWIFT’S GREATEST HITS
70. Automatic Reference Counting
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { print("Bye, (name)!") }
}
var p1: Person? = Person(name: "Chad")
p1 = nil // "Bye, Chad”
p1 = Person(name: "Murray")
var p2 = p1
p1 = nil // Murray still has an active reference
p2 = Person(name: "Bruce") // "Bye, Murray"
The “other” ARC
SWIFT’S GREATEST HITS
71. Automatic Reference Counting
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { print("Bye, (name)!") }
}
class Apartment {
let unit: String
init(unit: String) { self.unit = unit }
var tenant: Person?
deinit { print("Apartment (unit) is being deinitialized") }
}
The “other” ARC
SWIFT’S GREATEST HITS
72. Automatic Reference Counting
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { print("Bye, (name)!") }
}
class Apartment {
let unit: String
init(unit: String) { self.unit = unit }
var tenant: Person?
deinit { print("Apartment (unit) is being deinitialized") }
}
var p: Person? = Person(name: "Chad")
var a: Apartment? = Apartment(unit: "5C")
p?.apartment = a
a?.tenant = p
p = nil // a still has a reference
a = nil // p still has a reference
The “other” ARC
SWIFT’S GREATEST HITS
73. Automatic Reference Counting
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { print("Bye, (name)!") }
}
class Apartment {
let unit: String
init(unit: String) { self.unit = unit }
weak var tenant: Person?
deinit { print("Apartment (unit) is being deinitialized") }
}
var p: Person? = Person(name: "Chad")
var a: Apartment? = Apartment(unit: "5C")
p?.apartment = a
a?.tenant = p
The “other” ARC
SWIFT’S GREATEST HITS
74. Automatic Reference Counting
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { print("Bye, (name)!") }
}
class Apartment {
let unit: String
init(unit: String) { self.unit = unit }
weak var tenant: Person?
deinit { print("Apartment (unit) is being deinitialized") }
}
var p: Person? = Person(name: "Chad")
var a: Apartment? = Apartment(unit: "5C")
p?.apartment = a
a?.tenant = p
p = nil // "Bye, Chad!"
a = nil // "Apartment 5C is being deinitialized"
The “other” ARC
SWIFT’S GREATEST HITS
76. Swift Playgrounds
• For macOS & iPad
• Start playing with the full power of
swift in seconds
• Tons of tutorials and other
resources
• REPL on Steroids
84. Swift Playgrounds
• For macOS & iPad
• Start playing with the full power of
swift in seconds
• Tons of tutorials and other
resources
• REPL on Steroids
Swift forces you to address the fact that this value may or may not be nil
uses syntactic sugar
Rules apply to all types—primitives and other value types as well as reference types
Introduces a new way of thinking, particularly with regards to default values