swift type system

30

Upload: goichi-hirakawa

Post on 08-Jan-2017

1.605 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Swift Type System
Page 4: Swift Type System
Page 5: Swift Type System
Page 6: Swift Type System
Page 7: Swift Type System
Page 8: Swift Type System

“Type systems are generally formulated as collections of rules for checking the ‘consistency’ of programs.”

Benjamin C. Pierce

Page 9: Swift Type System
Page 10: Swift Type System
Page 11: Swift Type System
Page 12: Swift Type System
Page 13: Swift Type System
Page 14: Swift Type System

is, as?, as! (as )

$ swift Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance. 1> 1 is Any $R0: Bool = true 2> 1 is AnyObject $R1: Bool = false 3> import Foundation 4> 1 is Any $R2: Bool = true 5> 1 is AnyObject $R3: Bool = true // ???

Page 15: Swift Type System

is, as?, as! (as )

$ swift Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance. 1> [1] is [Any] $R0: Bool = true 2> [1] is [AnyObject] repl.swift:2:5: error: 'Bool' is not convertible to ‘[AnyObject]’ // ??? [1] is [AnyObject] ~~~~^~~~~~~~~~~~~~ 2> import Foundation 3> [1] is [Any] $R3: Bool = true 4> [1] is [AnyObject] $R4: Bool = true

Page 16: Swift Type System
Page 17: Swift Type System
Page 18: Swift Type System
Page 19: Swift Type System
Page 20: Swift Type System
Page 21: Swift Type System

nil

nil

Page 22: Swift Type System
Page 23: Swift Type System
Page 24: Swift Type System

$ ghci GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help … Prelude> let square x = x * x Prelude> :info square square :: Num a => a -> a Prelude> square 10 100

Page 25: Swift Type System

expression statement

// Objective-C NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] init];

// Swift let mutableAttributedString = NSMutableAttributedString()

Page 26: Swift Type System

• 3: Int (default), IntXX, UIntXX, Double, Float, …

• 3.14159: Double (default), Float, …

Page 27: Swift Type System

$ swift Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance. 1> let x1 = 3 x1: Int = 3 // inferred to be Int 2> let x2 = Double(3) x2: Double = 3 // inferred to be Double 3> let x3: Double = 3 x3: Double = 3 // explicit type specified

4> let y1 = 3.14159 y1: Double = 3.1415899999999999 // inferred to be Double 5> let y2 = Float(3.14159) y2: Float = 3.14159012 // inferred to be Float 6> let y3: Float = 3.14159 y3: Float = 3.14159012 // explicit type specified

Page 28: Swift Type System

import CoreGraphics

// func CGPointMake(_ x: CGFloat, _ y: CGFloat) -> CGPoint let x = CGFloat(1.0 / 3) // 1 / 3 = 0(?), 1.0 / 3 = 0.333 let y = CGFloat(1.0 / 3) // 1.0 / 3: Double to CGFloat let p = CGPointMake(x, y)

// CGFloat Swift // “1 / 3” “CGFloat / CGFloat -> CGFloat” // “1” “3” “CGFloat” let p = CGPointMake(1 / 3, 1 / 3)

Page 29: Swift Type System
Page 30: Swift Type System