languages • Built on LLVM optimizing compiler • ARC provides memory management without GC pauses • Concurrency provided via Grand Central Dispatch framework 7
2014 (WWDC) 1.0-beta September 2014 1.0 June 2015 (WWDC) 2.0-beta September 2015 2.0 December 2015 Swift open-sourced on GitHub June 2016 (WWDC) 3.0-beta September 2016 3.0
print("Quite nice") case "Strawberry", "Rum'n'raisin": print("Very nice") case let str where str.hasPrefix("Mint"): print("UGH!!!, I hate \(str)") default: print("No opinion about this") } 22
validName = name { print("Hello, \(validName)") } else { print("Anonymous, eh...") } let str = "42" let num = Int(str) if num != nil { print("Conversion successful - num is \(num!)") } if let num = Int(str) { print("Conversion successful - num is \(num)") } else { print("Conversion failed") }
return a + b } addInts(a: 1, b: 3) func addInts(_ a: Int, _ b: Int) -> Int { return a + b } addInts(1, 3) func move(from start: Point, to end: Point) -> Bool { /* code */ } move(from: a, to: b)
var min = numbers[0] var max = numbers[0] for number in numbers { if number > max { max = number } else if number < min { min = number } } return (min, max) } let result = minAndMax(1, 2, 3, 4, 5) print(result.min) print(result.max) 27
switch status { case .PendingOn(let approver): print("Request pending on approval from \(approver)") case .Denied: print("Request DENIED") case .Approved(let code): print("Request approved - auth code \(code)") } enum ApprovalStatus { case PendingOn(String) case Denied case Approved(String) }
APIs Kitura Linux Server Project Swagger REST API Deploy Deploy • Same module on client and server • Local or remote API calls • Automatically created Swagger APIs
App Bringing Swift to the Server Foundation Swift Swift Standard Library Core Foundation Dispatch PWQ C libs GLibc Foundation Swift Swift Standard Library Core Foundation Dispatch Darwin C libs Client-specific Libraries App Libraries Server-specific Libraries App Libraries Driving Towards Consistent Runtime across Clients/Servers Server-side Environments (Built with Foundation & Libdispatch)
into queue to execute dequeued tasks are being executed in wai8ng • Execute code concurrently on multicore hardware by submitting work to dispatch queues managed by the system.
Serial: work items executed one at a time • Parallel: work items dequeued in order but run simultaneously • Work: a Swift closure • Managed by the system • Work is executed on a thread pool managed by GCD • Work may execute on any thread 38
& Interfaces Swift Libraries SwiftyJSON SwiftMongoDB Swift Binary Foundation Package Manager C Libraries Dispatch HttpParser HiRedis CURL PCRE2 Pluggable Components
Sources │ └── main.swift └── Tests $ mkdir myProject && cd myProject 2. Next, initialize this project as a new Swift package 1. First, we create a new project directory $ swift package init --type executable Basic Swift package directory structure:
Kitura.run() let router = Router() router.get("/hello") { request, response, next in response.status(.OK).send("<h1>Hello, World!</h1>").end() } 6. Create and start an HTTPServer: 4. Add a router and a path: router.get("/hello.json") { request, response, next in response.status(.OK).send(json: JSON(["Hello": "World!"])).end() } 5. Add a JSON data route
= Router() router.get("/hello") { request, response, next in response.status(.OK).send("<h1>Hello, World!</h1>").end() } router.get("/hello.json") { request, response, next in response.status(.OK).send(json: JSON(["Hello": "World!"])).end() } Kitura.addHTTPServer(onPort: 8090, with: router) Kitura.run() 7. Sources/main.swift should now look like this:
of Foundation • Libdispatch performance and scalability improvements • Building a vibrant Swift on Linux community • Server-side packages in the rapidly-growing SPM ecosystem • IDE support • More cloud integration • Exploring the Swift “sweet-spot” • Linux-driven enhancements to Swift itself? 60