Top Banner
SWIFT! Marc Prud'hommeaux class Session { let title: String // constant non-optional field: can never be null and can never be changed var instruktør: Person? // variable optional field: null is permitted var attendees: [Person] = [] // array that can only contain Person instances var requirements: [String: Float] = [:] // dictionary of String to Float init(title: String) { self.title = title } // required initializer w/ named parameter func ready() -> Bool { return attendees.count > 0 } // functon with return value func begin() { println("Let's teach some Swift to \(attendees.count) eager learners!") } } struct Person { let firstName, lastName, email: String } // immutable structure with automatic init let sesh = Session(title: "Swift!") // class initializer // struct initializer sesh.instruktør = Person(firstName: "Marc", lastName: "Prud'hommeaux", email: "[email protected]") sesh.requirements["Mac OS"] = 10.10 // dictionary value assignment sesh.requirements["Xcode"] = 6.10 if training.ready() { // braces required, parenthesis optional training.begin() // semicolons optional }
75

SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Jul 09, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

SWIFT!

Marc Prud'hommeaux

class Session { let title: String // constant non-optional field: can never be null and can never be changed var instruktør: Person? // variable optional field: null is permitted var attendees: [Person] = [] // array that can only contain Person instances var requirements: [String: Float] = [:] // dictionary of String to Float

init(title: String) { self.title = title } // required initializer w/ named parameter

func ready() -> Bool { return attendees.count > 0 } // functon with return value func begin() { println("Let's teach some Swift to \(attendees.count) eager learners!") } }

struct Person { let firstName, lastName, email: String } // immutable structure with automatic init

let sesh = Session(title: "Swift!") // class initializer // struct initializer sesh.instruktør = Person(firstName: "Marc", lastName: "Prud'hommeaux", email: "[email protected]") sesh.requirements["Mac OS"] = 10.10 // dictionary value assignment sesh.requirements["Xcode"] = 6.10

if training.ready() { // braces required, parenthesis optional training.begin() // semicolons optional }

Page 2: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

About Me

• Marc Prud'hommeaux, American

• Indie Software Developer

• Neither French nor an Apple employee

Page 3: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Swift

• Announced at WWDC June 2014

• It is: “a new language for the future of Apple software development”

Page 4: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Let's Build a Calculator App!

• https://gist.github.com/mprudhom

Page 5: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Thank You!

• Marc Prud'hommeaux <[email protected]>

• Twitter: @mprudhom

• Apps: http://www.impathic.com

Page 6: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Agenda

• Swift vs. X Language Comparison

• Swift Language Features

• Swift Demo

Page 7: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Language Comparison

Page 8: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Objective-C

• Very different!

Page 9: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

ObjC vs. Swift

@interface Concatinator : NSObject @property NSString *separator; @end

@implementation Concatinator - (NSString *)concat:(NSString *)a withString:(NSString *)b { return [[a stringByAppendingString:self.separator] stringByAppendingString:b]; } @end

Page 10: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

ObjC vs. Swift

class Concatinator { var separator: String = ","

func concat(a: String, b: String) -> String { return a + separator + b } }

Page 11: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Java/C#

• Similar

Page 12: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Java/C# vs. Swift

class Concatinator { String separator = ",";

String concat(String a, String b) { return a + separator + b; } }

Page 13: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Java/C# vs. Swift

class Concatinator { var separator: String = ","

func concat(a: String, b: String) -> String { return a + separator + b } }

Page 14: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Scala

• Very similar!

Page 15: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Scala vs. Swift

class Concatinator { var separator: String = ","

def concat(a: String, b: String) : String = { return a + separator + b } }

Page 16: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Scala vs. Swift

class Concatinator { var separator: String = ","

func concat(a: String, b: String) -> String { return a + separator + b } }

Page 17: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Type Systems Landscape

static

dynamic

weak strong

Assmebly JS Ruby Python Clojure

Typescript

CJavaC# Scala

OCamlHaskellSwift

Objective-C

Slide Credit: Martin Odersky, "The Trouble with Types"

Page 18: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Language Features

Page 19: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

(Im)mutability

• let: constant

• var: variable

Page 20: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

let str: String = "Hello"

let vs. var

Page 21: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

let vs. var

let str: String = "Hello" str = str + " World" // illegal!

Page 22: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

let vs. var

var str: String = "Hello" str = str + " World" // legal

Page 23: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

let vs. var

var str: String = "Hello" str = nil // illegal!

Page 24: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Optionals

• Optionals permit nil-ability

• Designated with Type? or Optional<Type>

Page 25: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Optionals

var str: String = "Hello" str = nil // illegal!

Page 26: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Optionals

var str: String? = "Hello" str = nil // legal

Page 27: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Optionals

var str: Optional<String> = "Hello" str = nil // legal

Page 28: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Generics

• Allow the parameterization of types

Page 29: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Generics

var strings: Array<String> = ["a", "b", "c"]

Page 30: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Generics

var strmap: [String : Int] = ["a": 1, "b": 2, "c": 3]

Page 31: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Type Inference

• Compiler figures out the type for you

Page 32: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Basic Type Inference

var str: String = "abc"

Page 33: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Basic Type Inference

var str = "abc"

Page 34: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Basic Type Inference

var strs: Array<String> = ["a", "b", "c"]

Page 35: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Basic Type Inference

var strs = ["a", "b", "c"]

Page 36: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Basic Type Inference

var strmap: [String : Int] = ["a": 1, "b": 2, "c": 3]

Page 37: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Basic Type Inference

var strmap = ["a": 1, "b": 2, "c": 3]

Page 38: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Simple Type Inference

[1, 2, 3].reverse() .map({ Double($0) }) .filter({ $0 >= 2.0 }) .map({ Float($0) })

Page 39: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Simple Type Inference

let floats : Array<Float> = [1, 2, 3].reverse() .map({ Double($0) }) .filter({ $0 >= 2.0 }) .map({ Float($0) })

Page 40: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Simple Type Inference

let floats = [1, 2, 3].reverse() .map({ Double($0) }) .filter({ $0 >= 2.0 }) .map({ Float($0) })

Page 41: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Complex Type Inference

lazy([1, 2, 3]) .reverse() .map({ Double($0) }) .filter({ $0 >= 0 }) .map({ Float($0) })

Page 42: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Complex Type Inference

let mapped : LazySequence <MapSequenceView <FilterSequenceView <MapCollectionView <RandomAccessReverseView <[Int]>, Double>>, Float>> = lazy([1, 2, 3]) .reverse() .map({ Double($0) }) .filter({ $0 >= 0 }) .map({ Float($0) })

Page 43: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Complex Type Inference

let mapped = lazy([1, 2, 3]) .reverse() .map({ Double($0) }) .filter({ $0 >= 0 }) .map({ Float($0) })

Page 44: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Enumerations

• Fixed list of values

• With associated types!

Page 45: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Enums w/ Associated Types

enum Stuff<T> { case Nothing case Something(T) }

let stuff: Stuff = Stuff.Something("Hello")

switch stuff { case .Nothing: println("Nothing at all") case .Something(let value): println("Something: \(value)") }

Page 46: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Structs• structs are value types: they are copied when

passed around or re-assigned

• vs. classes, which are reference types

• Swift's built-in String, Array, and Dictionary are structs!

• Unlike Cocoa's built-in NSString, NSArray, NSDictionary, which are reference types

Page 47: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

class vs. struct

class Concatinator { var separator: String = ","

func concat(a: String, b: String) -> String { return a + separator + b } }

Page 48: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

class vs. struct

struct Concatinator { var separator: String = ","

func concat(a: String, b: String) -> String { return a + separator + b } }

Page 49: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Functions & Closures

• func keyword

• First-class types!

• Functions within functions

• Functions returning functions

• Functions accepting functions arguments

Page 50: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Functions & Closures

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

let sum = add(1, 2)

Page 51: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Functions & Closures

let add : (Int, Int) -> Int = { (a, b) in return a + b }

let sum = add(1, 2)

Page 52: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Functions & Closures

let add : (Int, Int) -> Int = { (a, b) in return a + b }

let sum = add(1, 2)

Page 53: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Other Features

• Tuples (anonymous structs)

• Operator Overloading (operators as functions)

• Named parameters & default parameter values

• Pattern Matching ("switch on steroids")

• Function Currying (Haskell might be proud)

Page 54: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Shold I Adopt Swift?Pros Cons

Familiar Syntax for Java/C# devs Unfamilliar to ObjC devs

Faster Data Structures Inflexible Runtime

Staticly Typed Not Dynamically Typed

Playgrounds & REPL Immature Tooling

Good ObjC & C interop No C++ or Assembly interop

Functional Style Functional Style

Mature Runtime Immature Tooling

It's the Future (probably)

Page 55: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Environment

Page 56: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Runtime

• Same as Objective-C Runtime

• Shared memory with Cocoa classes

• Automatic Reference Counting (ARC)

Page 57: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Cocoa Interoperability

• Swift can access all Cocoa Frameworks

• Objective-C can access some Swift code

Page 58: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Development Environment

• Xcode

• Swift REPL

• Swift Playgrounds

Page 59: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Playground Demo

Page 60: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

import Darwin

struct Calc<T> { var lt: () -> T var op: (T, T) -> T var rt: () -> T

init(lt: () -> T, op: (T, T) -> T, rt: () -> T) { self.lt = lt self.op = op self.rt = rt }

init(const: T) { self.init(lt: { const }, op: { lt, rt in lt }, rt: { const }) }

var run: (() -> T) { return { self.op(self.lt(), self.rt()) } }

mutating func push(op: (T, T)->T, value: T) { self = Calc(lt: self.run, op: op, rt: { value }) }

}

infix operator ** { associativity left precedence 170 }

func ** (num: Double, power: Double) -> Double { return pow(num, power) }

var clc = Calc(const: 1.0) clc.push(+, value: 2) clc.push(**, value: 3)

clc.run()

Page 61: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Swift Adoption Considerations

Page 62: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Immature Tooling

• Xcode

• SourceKit!

Page 63: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Uncertain Future

• Apple's history: Java, Python, Ruby, GC

Page 64: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Compiler Limitations• enums with associated types: "unimplemented IR

generation feature non-fixed multi-payload enum layout"

• class variables: "class variables not yet supported"

• no currying struct methods: "partial application of struct method is not allowed"

• perplexing error messages: "'() -> T' is not a subtype of 'UInt8'"

Page 65: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Future Compatibility

• Compiled Swift code will continue to run

• Language syntax will change

Page 66: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

No Garbage Collection

• ARC requires that you manually break reference cycles

• ...or declare one side to be weak or unowned

Page 67: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Error Handling

• There is none!

Page 68: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Concurrency

• None!

• No locks, no synchronized, no nothing

Page 69: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Closedness

• LLVM & clang are open-source

• Swift standard library is not

Page 70: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Non-Portability

• Unlikely to be running on non-Apple devices anytime soon

Page 71: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Static vs. Dynamic

• Swift eschews Objective-C's dynamic dispatch

• No message passing

• No objc_msgsend

Page 72: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

No C++/Assembly Interop

• Swift interoperates with Objective-C & C

• Does not interoperate with C++ or assembly

Page 73: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Objective-C to Swift interoperability

• Swift fully interoperates with Objective-C

• Objective-C partially interoperates with Swift

Page 74: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

OS Support

• iOS 7+

• MacOS 10.10+

Page 75: SWIFT! - slides.yowconference.com · Swift Objective-C Slide Credit: Martin Odersky, "The Trouble with Types" Language Features (Im)mutability

Thank You!

• Marc Prud'hommeaux <[email protected]>

• Twitter: @mprudhom

• Apps: http://www.impathic.com