swift rocks! #1

80
Swift Rocks! Anton Mironov Software Engineer at 1

Upload: hackraft

Post on 18-Aug-2015

10 views

Category:

Technology


3 download

TRANSCRIPT

Swift Rocks!

Anton Mironov Software Engineer at 1

Everything started with

Low Level Virtual Machine

2

Recent Objective-C features

• Blocks (closures)

• Automatic Reference Counting

• Literals

3

Swift is a next next step

4

Genius behind all of this

PhD Chris Lattner @clattner_llvm

5

Swift is based on• Objective-C

• Haskel

• Ruby

• Python

• C#

• and many others

6

First Lines of Code

print("Hello World!")

7

First Lines of Code

print("\(name), I am your father")

8

First Lines of Code

var name = "Luke" print("\(name), I am your father")

9

First Lines of Code

var name = "Luke" let role = "father" print("\(name), I am your \(role)")

10

Operators

let a = 3 let b = 4 let sum = a + b

11

Operators

infix operator + { associativity left precedence 140 }

func +(lhs: Int, rhs: Int) -> Int

12

No Autocasting

let a = 3 let b = 4.5 let sum = a + b

13

No Autocasting

let a = 3 let b = 4.5 let sum = Double(a) + b

14

Auto casting can be EvilNSEventModifierFlags modifierFlags = NSApp.currentEvent.modifierFlags;

BOOL isControlKeyPressed = modifierFlags & NSControlKeyMask;

if (isControlKeyPressed) { NSLog(@"Control pressed"); } else { NSLog(@"Control NOT pressed"); }

15

Branching

if 0 == number % 2 { print("even") } else { print("odd") }

16

Branching

let string = (0 == number % 2) ? "even" : "odd"

17

Branchingswitch numberOfApples { case 0: return "none" case 6...10: return "just right" case 42, -1: return "let me see" case let value where (1 == value % 2): return "that's odd" default: return "this is unacceptable" } 18

Collections: Array

let arrayOfInts = [5, 4, 3, 2, 1]

19

Collections: Set

let setOfInts: Set<Int> = [ 5, 4, 3, 2, 1 ]

20

Collections: Dictionary

let dictionaryOfNamesByDigit = [ 5: "five", 4: "four", 3: "three", 2: "two", 1: "one" ]

21

Cycles: Precondition

var i = 0 while i < 5 { print("value \(i)") i++ }

22

Cycles: Postcondition

var i = 0 repeat { print("value \(i)") i++ } while i < 5

23

Cycles: Counter

for var i = 0; i < 5; i++ { print("value \(i)") }

24

Cycles: Enumeration

let arrayOfInts = [5, 4, 3, 2, 1] for value in arrayOfInts { print("\(value)") }

25

Objective-C: Null Object Pattern

id operation = nil; [operation start];

26

Swift: Optionals

let operation: NSOperation? = nil operation?.start()

27

Swift: Optionals

if nil != person { return "Hello \(person!)" } else { return "Hello to nobody" }

28

Swift: Optionals

if let nonNullPerson = person { return "Hello \(nonNullPerson)" } else { return "Hello to nobody" }

29

Swift is Great for Imperative Programming

30

Functionsfunc name(args) -> ReturnType { … }

31

Functions: named arguments

func buildMessageForPerson( person: String, role: String) -> String { return "\(name), I am your \(role)" }

let message = buildMessageForPerson("Luke", role: "father" )

32

Functions: default valuesfunc buildMessageForPerson( person: String = "Luke", role: String) -> String { return "\(person), I am your \(role)" }

let message = buildMessageForPerson( role: "father" )

33

Functions: multiple returnfunc divide( a: Int, _ b: Int ) -> (result: Int, modulo: Int) { return (a / b, a % b) }

let result = divide(11, 4).result let modulo = divide(11, 4).modulo

34

Functions are First Class Citizen

35

Functions as arguments Simple Example

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

func runMathOp(a: Int, _ b: Int, op: (Int, Int) -> Int ) -> Int { return op(a, b) }

let value1 = runMathOp(4, 3, op: add) let value2 = runMathOp(4, 3, op: mul)

36

Functions as arguments Simple Example

func runMathOp(a: Int, _ b: Int, op: (Int, Int) -> Int ) -> Int { return op(a, b) }

let value1 = runMathOp(4, 3, op: +) let value2 = runMathOp(4, 3, op: -)

37

Swift is Functional Language

38

Imperative Stylelet names = ["Luke", "Chuwy", "Baker"] var messages = [String]()

for name in names { let message = buildMessageForPerson(name, "father") messages.append(message) }

print("\(messages)")

39

Functional Stylelet names = ["Luke", "Chuwy", "Baker"] let messages = names.map { buildMessageForPerson($0, "father") }

print("\(messages)")

40

Closuresfunc buildOperation(multiplier: Int ) -> (Int -> Int) { func multiply(value: Int) -> Int { return value * multiplier }

return multiply }

let operation = buildOperation(3) let value = operation(4)

41

Just use Functional Paradigm

42

Type System

43

Type System Classification Criteria: Parameters Passing and Ownership• By Value let a = 3

• By Sharing (by reference to shared object) let userDefaults = NSUserDefaults.standardUserDefaults()

• By Reference let operation = myFunction let c = operation(a, b)

44

Type System Classification Criteria: OOP

• Encapsulation

• Inheritance

• Polymorphism

45

Type System Classification Criteria: Access Control

• Public - visible for importers

• Internal (default) - visible within module

• Private - visible within file

46

Type System Classification Criteria: Objective-C compatibility• partial

• none

47

Types• tuple

48

Types• tuple

• function

49

Classesclass Transport {

} 50

Classesclass Transport { let name: String

} 51

Classesclass Transport { let name: String private var velocity: Double = 0.0

} 52

Classesclass Transport { let name: String private var velocity: Double = 0.0 init(name: String) { self.name = name }

} 53

Classesclass Transport { let name: String private var velocity: Double = 0.0 init(name: String) { self.name = name } func beep() { print("Beep!") } } 54

Classeslet transport = Transport( name: "kinda boat" )

55

Classeslet transport = Transport( name: "kinda boat" ) transport.beep()

56

Classeslet transport = Transport( name: "kinda boat" ) transport.beep() transport.speed = 10.0

57

Classesclass Car: Transport { var color: String = "Red" }

58

Classesclass Car: Transport, Paintable, Movable { var color: String = "Red"

… }

59

Classes Strong Referenceclass Person { var transport: Transport }

60

Classes Weak Referenceclass Car: Transport { weak var passanger: Passanger? }

61

Classes Unowned Reference

class Node { unowned var parentNode: Node }

62

Structuresstruct Point2D { var x: Double var y: Double

init(x: Double, y: Double) { self.x = x self.y = y } }

63

Structuresvar point1 = Point2D(x: 1.0, y: 2.0) var point2 = point1 point1.x = 3.0

// (point1.x != point2.x)

64

Structureslet point1 = Point2D(x: 1.0, y: 2.0) // point1 is immutable

65

Structuresstruct Point2D { var x: Double var y: Double … mutating func multiply(

mult: Double) { self.x *= mult self.y *= mult } }

66

Structures are everywhere• Int, Bool, UInt64, Float, Double, …

• String

• Array, Set, Dictionary

• …

67

Enumsenum TokenStatus: String { case None = "none" case Valid = "valid" case Invalid = "invalid" case Expired = "expired"

var shouldRefetch: Bool { return .Valid == self } }

68

Enums (Unions)enum FileSystemError: ErrorType { case InvalidURL(NSURL) case InvalidPermissions case UnknownErrorCode(Int) }

69

Enums (Unions)switch fileSystemError { case .InvalidURL(let URL): print("Invalid URL \(URL)") case .InvalidPermissions: print("Invalid permissions") case .UnknownErrorCode(let code) where code == -150: print("I've seen this error somewhere") case .UnknownErrorCode(let code): print("Something gone very wrong \(code)") }

70

Protocolsprotocol Drawable { var boundingRect: NSRect { get } func draw() }

struct Polygon: Drawable { var points = [NSPoint]() //MARK: Drawable var boundingRect: NSRect? { ... } func draw() { ... } } 71

Generic Functionsfunc min<T: Comparable>( lhs: T, _ rhs: T ) -> T { return lhs < rhs ? lhs : rhs }

72

Generic Typesstruct Point<T: FloatingPointType> { var x: T var y: T }

73

Extensionsextension NSDate { var myBirthday: NSDate { … } }

74

Protocols + Extensions + Generics = ❤

75

Swift

Objective-C

76

Simple or Complex?

77

Swift on Prod

78

Just use It!

79

Thank you!

Anton Mironov Software Engineer at [email protected]

80