33
What’s new in Swift 3? Pushkar N Kulkarni | @pushkar_nk | pushkarnk IBM Runtimes IBM India Software Lab

Swift Bengaluru Meetup slides

Embed Size (px)

Citation preview

What’s new in Swift 3?

Pushkar N Kulkarni | @pushkar_nk | pushkarnkIBM Runtimes

IBM India Software Lab

Agenda• Swift@IBM - a server-side effort

• Language changes in Swift 3.0 (hands-on)

• Swift Package Manager (hands-on)

Swift@IBM• Swift on Linux – swift.org

• IBM Swift Package Catalog

• Kitura

• IBM Swift Sandbox

Blog: https://developer.ibm.com/swift/blogs/

Swift on Linux

5

IBM Swift Package Catalog• https://swiftpkgs.ng.bluemix.net/

• Find, explore and share packagesfrom the open-source Swiftecosystem

• Explore dependencies

• Submit your own packages

6

Kitura Web FrameworkWhat is it?• New, modular, package-based web framework written in

Swift

Why is this cool?• Empower a new generation of native mobile developers

to write and deploy code into the Cloud.

Developer Benefits ?• Delivers core technologies needed to stand up

enterprise apps on the server• Enables developers to create a web

application in Swift and deploy these servers on Linux and the Cloud.

http://github.com/ibm-swift/kitura

7

IBM Swift Sandbox

• Interactive sandbox forrapid prototyping andexperimentation in Swift

• Saves your work

• Supports multiple versions of Swift

• Responsive design

https://swiftlang.ng.bluemix.net/

Swift 3.0• Major release

• Not source-compatible with Swift 2.2

• Fundamental changes to the language and standard library

• Swift package manager – Linux & Darwin

Language changes• Removal of C-style for-loops

• Removal of increment/decrement operators

• New function to find first element passing a predicate

• Consistent label behavior for first parameter

Language changes• New API guidelines

• Keywords in member references

• Case labels with multiple patterns

• Generic type-aliasing

C-Style code (Swift 2.2)func arithmeticSeries(a: Int, d: Int, n: Int) -> [Int] { var series: [Int] = [] for var i = 0; i < n; i++ { series += [a + i*d] } return series}

arithmeticSeries(1, d: 3, n: 10)

C-style constructs removed //this works on all Swift versionsfunc arithmeticSeries(a: Int, d: Int, n: Int) -> [Int] { var series: [Int] = [] for i in 0..<n { series += [a+i*d] } return series}

print(arithmeticSeries(1, d: 3, n: 10))

Sequence iteration - stdlibfunc arithmeticSeries(a: Int, d: Int, n: Int) -> [Int] { var series: [Int] = [] for x in sequence(first: a, next: {$0+d}).prefix(n){ series += [x] } return series}

print(arithmeticSeries(a: 1, d: 3, n: 10))

Sequence.first(where:)Finding the first element that satisfies a predicate

let series = arithmeticSeries(a: 1, d: 3, n: 10)//first double digit elementseries.first(where: {$0 > 9})

Consistent label behavior for first parameter

Until Swift 2.2

func sum (x a: Int, y b: Int, z c: Int) -> Int { return a + b + c}

>> sum (x: 10, y: 20, z: 30)

Consistent label behavior for first parameter

Until Swift 2.2

func sum (a: Int, y b: Int, z c: Int) -> Int { return a + b + c}

>> sum (10, y: 20, z: 30)

Consistent label behavior for first parameter

Until Swift 2.2

func sum (a: Int, b: Int, c: Int) -> Int { return a + b + c}

>> sum (10, b: 20, c: 30)

Consistent label behavior for first parameter

In Swift 3.0

func sum (a: Int, b: Int, c: Int) -> Int { return a + b + c}

>> sum (a: 10, b: 20, c: 30)

Consistent label behavior for first parameter

In Swift 3.0 – to save your clients/users from doing extra work

func sum (_ a: Int, b: Int, c: Int) -> Int { return a + b + c}

>> sum (10, b: 20, c: 30)

Power of Enumspublic enum Video { case Public(Int, Int, String) case Private(String) case Optional(String, Int)}

Power of Enumspublic enum Video { case Public(Int, Int, String) case Private(String) case Optional(String, Int)}

Power of Enumsextension Video { func getName() -> String { switch self { case let .Public(_, _, s) : return s case let .Private(s): return s case let .Optional(s, _): return s } }}

Lower camel case – New API guideline

public enum Video { case public(Int, Int, String) case private(String) case optional(String, Int)}

public enum Video { case `public`(Int, Int, String) case `private`(String) case `optional`(String, Int)}

Keywords in member referencesextension Video { func getName() -> String { switch self { case let .public(_, _, s) : return s case let .private(s): return s case let .optional(s, _): return s } }}//backticks not needed while referencing member!

Case labels with multiple patternsextension Video { func getName() -> String { switch self { case let .public(_, _, s), let .private(s), let .optional(s, _): return s } }}

Reversing a tripletfunc reverse(t: (Int, Int, Int)) -> (Int, Int, Int) { return (t.2, t.1, t.0)}

reverse(t: (1,2,3))

func reverse(t: (Double, Double, Double)) -> (Double, Double, Double) { return (t.2, t.1, t.0)}

reverse(t: (1.1, 2.3, 3.1))

Reversing a triplettypealias IntTriplet = (Int, Int, Int)typealias DoubleTriplet = (Double, Double, Double)

func reverse(t: IntTriplet) -> IntTriplet { return (t.2, t.1, t.0)}

func reverse(t: DoubleTriplet) -> DoubleTriplet { return (t.2, t.1, t.0)}

Generic Typealiasestypealias Triplet<T> = (T, T, T)

func reverse<T>(t: Triplet<T>) -> Triplet<T> { return (t.2, t.1, t.0)}

reverse(t: (1,2,3)reverse(t: (1.1,2.2,3.2)

Generic Type aliases

Will your function reverse function reverse (“swift”, 3.14, 100) ?

How would you do that?

Swift Package ManagerA successful modern language needs an efficient package manager

• A tool for managing distribution of Swift code• Integrated with the Swift build system• Downloading, compiling and linking dependencies• Baby step towards matching CocoaPods & Carthage

Swift Package Manager• Modules• Packages

Manifest fileGit based for now

• ProductsLibrary or executable

• DependenciesSemantic version management

Package.swift

import PackageDescription

let package = Package( name: ”Reverser", targets: [], dependencies: [ .Package(url: "https://github.com/apple/example-package.git", majorVersion: 1), ])

Thank you!

Twitter - @pushkar_nk

GitHub - pushkarnk